andPat
andPat

Reputation: 4503

Rename multiple files in bash

I have A.js, B.js, C.js in a certain directory and I want to write a SINGLE command line in bash shell to rename these files _A, _B, _C. How can I do this?

I tried find -name '*.sh' | xargs -I file mv file basename file .sh but it doesn't work, basename file .sh isn't recognized as a nested command

Upvotes: 4

Views: 10530

Answers (3)

dtrckd
dtrckd

Reputation: 662

A simple native way to do it, with directory traversal:

find -type f | xargs -I {} mv {} {}.txt

Will rename every file in place adding extension .txt at the end.

And a more general cool way with parallelization:

find -name "file*.p" | parallel 'f="{}" ; mv -- {} ${f:0:4}change_between${f:8}'

Upvotes: 0

anishsane
anishsane

Reputation: 20970

How about

rename 's/(.*).js/_$1/' *.js

Check the syntax for rename on your system.

The above command will rename A.js to _A & so on.

If you want to retain the extension, below should help:

rename 's/(.*)/_$1/' *.js

Upvotes: 8

Memento Mori
Memento Mori

Reputation: 3402

Assuming you still want to keep the extension on the files, you could do this:

$ for f in * ; do mv "$f" _"$f" ; done

It will get the name of each file in the directory, and prepend an "_".

Upvotes: 4

Related Questions