How to rename multiple files from one extension to another on Linux / Unix?

I have several files that end in ".1", for example:

example.file.ex1.1 example.file.ex2.1 example.file.ex3.1 

Is there a way that I can quickly rename them all without ".1" at the end (for example, example.file.ex1, example.file.ex2, etc.)?

Thanks!

+8
linux unix file-rename
source share
3 answers

Pure bash solution:

 for curFile in example.file.*.1; do mv -- "$curFile" "${curFile:0:-2}" done 
+5
source share

Yes, try this by renaming :

 rename -n 's/\.1$//' * 

remove -n (idle switch) if your tests are correct.

warning There are other tools with the same name that may or may not do this, so be careful.


If you run the following command ( linux )

 $ file $(readlink -f $(type -p rename)) 

and you have a result like

 .../rename: Perl script, ASCII text executable 

then this seems to be the right tool =)

If not, make it the default (usually already) in Debian and its derivative, for example Ubuntu :

 $ sudo update-alternatives --set rename /path/to/rename 

Last but not least, this tool was originally written by Larry Wall, the father of Perl.

+12
source share

Another bash using parameter extension :

 for curFile in example.file.*.1; do mv "$curFile" "${curFile%.1}" done 
+3
source share

All Articles