Linux: rename or change extension of multiple files

Posted About 13 years ago. Visible to the public.

When you need to bulk rename files you can not call "mv *.foo *.bar" to change the extension of all .foo files to bar (because bash resolves wildcards and replaces them with the list of matched files).

This works on linuxes who use the Perl version of the rename command (like Ubuntu):

rename 's/\.foo$/\.bar/' *

You can also use this to rename other parts of the file, e.g. from flag_en.png, flag_de.png etc. to just en.png or de.png:

rename 's/^flag_//' *

Note that we used $ and ^ to explicitly look at the end and start of the filenames.

Arne Hartherz
Last edit
Over 7 years ago
Henning Koch
Keywords
bash, shell
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-02-16 14:31)