Thursday 3 November 2011

Batch renaming files in Ubuntu Linux

I often deal with a large number of data files, for example files containing ultrasound data from experiments. Recently I had to rename about 200 files which were named something like this,

oldName001.mat, oldName002.mat, ..., oldName200.mat

to something like this:

newName001.mat, newName002.mat, ..., newName200.mat

Basically, I wanted to keep the numbering but change the base of the name. I found out that on Ubuntu 11.10, which I'm currently using, there are two different flavors of the "rename" command. One is a Perl script, and using this, the syntax for doing the above operation is

rename "s/oldName/newName/" *mat

To test without actually renaming, use the "-n" switch. Type "man rename" or have a look at these Webmaster Tips for several more examples of this usage. However, there is also a rename command as part of the linux-util package. To distinguish this from the above command in Ubuntu, call the command as rename.ul:

rename.ul oldName newName *mat

Notice that the syntax is simpler and easier to remember for this version. Type "man rename.ul" or have a look at this NixCraft post for more examples. 

Finally I found out that it is also possible to batch rename files using Thunar, which is the default file manager in Ubuntu 11.10. Mark all the files, right-click and choose "rename", and use the "Search and replace" option.

0 comments:

Post a Comment