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.