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

user2716340 picture user2716340 · Aug 26, 2013 · Viewed 9.4k times · Source

I have a number of files that end in a '.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 the '.1' at the end (e.g. example.file.ex1, example.file.ex2, etc.)?

Thanks!

Answer

Gilles Quenot picture Gilles Quenot · Aug 26, 2013

Yes, try this with rename :

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

remove the -n (dry-run mode switch) if your tests are valid.

warning There are other tools with the same name which may or may not be able to 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, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

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

Last but not least, this tool was originally written by Larry Wall, the Perl's dad.