How to rename a batch of files in Linux

Bulk renaming files can be done with the rename command. It shares many similarities with cp and mv, but its simplicity can be so staggering that its difficult to figure out how to use it.

If we just type “rename” at the command prompt, all we get is the message

rename
call: rename from to files...

While technically correct, what on earth does it mean? How do we use rename?

Let’s do a little exercise. Imagine we had a batch of files, perhaps something like “Title 101.mp4” to “Title 110.mp4”. Let’s create some empty files with those names in a test directory:

mkdir test
cd ./test
touch 'Title '{101..110}.mp4
ls

So far so good. Now we’d like to rename those files so they read “New Title 101.mp4” to “New Title 110.mp4”. Here’s how it works:

rename 'Title' 'New Title' *.mp4

Technically, this follows what the command showed us earlier: “rename from to files…”. Still I feel a little explanation is in order.

For the rename command to work, we don’t need to specify the full file name, nor that we want to rename a batch of files. The command will rename anything that it encounters. All it needs to know is which string to replace with which other string. Those are the first two parameters we give it, in our case wrapped in single quotes because we have a space character in our titles.

The third parameter tells rename where the files live that we want to rename. In our example it was here in the current directory, but it could be anywhere on the system. By specifying *.mp4, only files with that ending will be renamed, all other files will be left in peace.

I hope this helps to understand rename a little better.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices.

He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.