Batch rename files in Linux

Is there any easy ways to batch rename files in Linux? A common requirement is that the web developers may ask you to help rename all *.html files to *.htm, how can we do that?

Someone probably will say that they can make a simple script in some minutes to do this kind of things. Actually you don’t need to recreate the wheel, the operating system have supplied such an utility already, the command is rename, it’s part of the util-linux package.

With the help of this command, it’s a very easy work to bulk rename files.
But, note that this command is a little different by systems.
Here we’ll show the usage examples for rename in both Redhat / CentOS and Debian / Ubuntu.

1, Batch rename files in Redhat / CentOS
For the scenario we mentioned above, it can be done like this:

$ rename .html .htm *

It renames all files in current working directory that match *.html* to *.htm*.
Note that only the first matched occurrence will be modified. If there’s a file called abc.html.abc.html, after renamed once, the new name is abc.htm.abc.html, not abc.html.abc.htm.

2, Bulk rename files in Debian / Ubuntu
The command here in Debian are quite different which makes it more powerful and professional.
I’ll show two examples with explaination:

$ rename -n 's/.html$/htm/' *

-n means no action will be applied, it will shows detailed information on what files would have been renamed:

$ rename -n 's/.html$/.htm/' abc.html.abc.html
abc.html.abc.html renamed as abc.html.abc.htm

The expression regunation here is in Perl style, which means you can apply multiple actions at one time, like this:

$ rename 's/.html$/htm/, s/css/css.backup/, s/jscript/js/' *

For more details you can raise a forum thread here at our support forum.

Share Button

5 thoughts on “Batch rename files in Linux

  1. Hi,

    How can one batch rename files from the command line. E.g. if there’s a bunch of files called a1.jpg, a2.jpg, a3.jpg, a4.jpg, a5.jpg etc…rename all the a’s to b’s or something of the sort.

    Thanks!
    Rename Files

  2. Just wanted to say I really liked the post. You have really put a lot of time into your posts and it is just wonderfull!

  3. “Krojamsoft BatchRename” Tool is a powerful tool, that allows you to quickly rename all the files in a specified directory. You can remove spaces, replace spaces with underscore, uppercase/lowercase filename, add a prefix/suffix, remove/replace strings and also catalog files by adding an incremental number to the file name.

Leave a comment

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