Rename multiple files in bash

I have A.js , B.js , C.js in a specific directory, and I want to write the SINGLE command line in the 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 find -name '*.sh' | xargs -I file mv file basename file .sh , but it does not work, the basename.sh file is not recognized as a nested command

+4
source share
3 answers

What about

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

Check the renaming syntax on your system.

The above command will rename A.js to _A etc.

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

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

Assuming you still want to save the extension in files, you can do this:

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

It will get the name of each file in the directory and add "_".

+4
source

An easy way to do this is through directory traversal :

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

Rename each file by adding the .txt extension to the end.

And a more general cool way to parallelize :

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

Source: https://habr.com/ru/post/1468913/


All Articles