Sometime back, we discussed gpRename — a graphical utility for renaming files. Though it provides lots of options for renaming files but I always felt the need of a command line utility that could at-least do the basic work of renaming more than one files in a go. I finally found the utility rename through which batch renaming of files can be done easily.
Rename – Batch Rename Through Command Line
A Snapshot From The MAN Page Of Rename
Here are the details of the testing environment used for this article :
- OS – Ubuntu 13.04
- Shell – Bash 4.2.45
- Perl – v5.14.2
A Brief Tutorial
The syntax of rename command is :
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
So, you can see that apart from optional arguments, the mandatory argument is a perl expression that tells rename command the actual work it is required to do. To get a better understanding of this utility, lets discuss some examples of it.
1. A Basic Example
Suppose you have some files with extension .out :
Now, if you want to change the extension of all these files from .out to .bak. You can use the rename command in the following way :
In the example above :
- We used two arguments.
- First argument is the perl expression that says — substitute .out with .bak. (If you want to learn perl substitutions — start here)
- Second argument lets the rename command know which files should be picked up for this substitution. So, in this case, we mentioned all the files with .out as extension i.e., *.out.
After the above command was executed, we confirmed the results :
So you can see that all the files with .out extension now have .bak extension.
2. Test Before The Actual Rename Operation Through -n Option
While doing some complicated or major renaming operations, you can also test the changes by running rename command with -n option. This lets you see what changes would actually be done but the changes are not done for real.
Here is an example :
Observe that the command displays the changes in the output but the changes are actually not done.
3. Get Verbose Output Through -v option
In example -1 we saw that rename command did not produce any information related to the changes it did. So, if you want details of the rename operation (just like -n option produced in example -2), use -v option. This would let rename command produce details of all the changes that it does in a particular run.
Here is an example :
So you can see that the above command substituted .bak with .out for all the files having .bak extension. The point worth noting is that all the changes were detailed in the output.
As -n was not used, so the changes were actually done in this case. Here is the proof :
So you can see that the changes were actually done.
4. Change Lower Case To Upper Case And Vice-Versa
I am covering this example as this is a popular use-case where batch renaming is required. So, to batch rename files with upper case names to lower case, just use y/A-Z/a-z/ as the perl expression.
Here is an example :
So we have these files with name in upper case characters. Now, to change these to lower case characters, run the following command :
Once the above command was executed, we checked for the changes done :
So you can see that it actually renamed all the upper case file names (with .OUT extension) to lower case. Similarly, lower case characters can be converted to upper case.
5. Overwrite Existing Files Through -f Option
By default, rename command won’t let you overwrite an existing file :
You can use -f option to overwrite existing files while using rename command.
So you can see that b.out (in this case) was overwritten only when -f option was used.
If you would like to know more about this command, here is a great tutorial.
Rename command is a perl script and it resides at /usr/bin/ on my system.
$ which rename /usr/bin/rename
You can download this script (.tar.gz) from here. Once done, run the following commands :
$ tar xvzf [tar-package] $ cd [main-folder] $ perl Makefile.PL $ make $ sudo make install
Please note that [tar-package] in my case was rename-1.6.tar.gz and [main-folder] was rename-1.6.
- Being a command line utility, it provides quick alternative for batch renaming
- I really like the test drive featured that can be enabled through -n option (example -2)
- You need to learn basic perl for framing perl expressions for this command.
- Does not come pre-installed in most of the Linux distributions.
If you are a system administrator or your work involves renaming files in bulk from command line then this utility is definitely for you. Try it out, it does what it actually promises.
Have you ever used rename or any similar command? Share your experience with us.