Rename – A Command Line Utility For Batch Renaming Of Files

By | 04/10/2013

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

rename-main

A Snapshot From The MAN Page Of Rename

Testing Environment

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 :

rename-1

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 :

rename-2

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 :

rename-3

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 :

rename-4

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 :

rename-5

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 :

rename-6

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 :

rename-7

So we have these files with name in upper case characters. Now, to change these to lower case characters, run the following command :

rename-8

Once the above command was executed, we checked for the changes done :

rename-9

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 :

rename-10

You can use -f option to overwrite existing files while using rename command.

rename-1

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.

Download/Install

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.

Pros

  • 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)

Cons

  • You need to learn basic perl for framing perl expressions for this command.
  • Does not come pre-installed in most of the Linux distributions.

Conclusion

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.

4 thoughts on “Rename – A Command Line Utility For Batch Renaming Of Files

  1. G.Bitzer

    Hi,

    there is another cmdline tool named “mmv”. It offers much more features than rename, and it uses standard regexps. Usually it is in a separate package which has to be installed, but I believe that it is in the repositories of the common distros .

    HtH

    Reply
    1. Himanshu Arora Post author

      @G.Bitzer

      Thanks a lot for sharing this tool. I’ll definitely explore it and if possible, would come up with a nice little article.

      Reply
  2. Sum Yung Gai

    Yep, I was already familiar with mmv, but I wasn’t familiar with rename until this article. It’s good to have more than one tool to do something, e. g. ffmpeg2theora vs. avconv. It’s even better when people like Mr. Arora call our attention to them. :-)

    Thanks!

    –SYG

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *