Spaghetti Programming

Rename Multiple Files at Once

Renaming multiple files at once is something that is not done too often, but when it does come up it is a huge help to use a tool like Larry Wall's rename command. To use this simple utility, you can give rename a Perl regular expression and it will perform some very powerful file operations with just a single command. Since I don't often need to rename many files at once, I always forget how to use this utility and then end up having to look it up. Here are some examples that I find the most useful.

To change the names of all files in a directory to contain only lower case letters you simply do:

rename 'y/A-Z/a-z/' *

Doing the reverse and changing all lower case letters to capital case letters should be obvious:

rename 'y/a-z/A-Z/' *

To replace all spaces in file names with an underscore:

rename 'y/ /_/' *

Obviously be careful with this, especially when using wildcarded file names. You don't want to inadvertently rename the wrong files!

rename multiple files at once

All of this information can also be found in the man page for rename.