
How can I batch rename files in Linux?
There are tons of how-tos and guides on using sed, mv, and other commands to batch rename files. However, a very powerful tool available in Debian and Ubuntu Linux is simply rename.
The rename command can be used to mass / bulk rename files, file extensions, rename files to lowercase, or other various tidbits your command-fu can dream up with perl expressions.
Here is a simple guide to get you started. (Note these work with Debian / Ubuntu perl version of rename. YMMV if you are using another distro or OS!)
Rename a directory of .jpeg files to .jpg:
$ rename 's/\.jpeg/.jpg/' *.jpeg
From the man page, here is a way to rename a directory of files to all lowercase:
$ rename 'y/A-Z/a-z/' *
This replaces spaces in filenames with underscores:
$ rename 'y/ /_/' *
This for example will replace the letters abcd with wxyz:
$ rename 's/abcd/wxyz/' *
abcd_1-2011.txt -> wxyz_1-2011.txt
Rename those DSCN6249.JPG photos off your camera to something more desirable:
$ rename 's/DSCN/party/' *
Sweet!
Fedora users, try something like this:
$ rename abcd wxyz *
Results:
abcd_1-2011.txt -> wxyz_1-2011.txt
Very Helpful this site for me..
Thanks….
I’m definitely ‘geeking out’ here! I put the commands to work and they put a smile on my face! Thanks Scott!
This seems to have an even simpler syntax on my CentOS 5.5 server:
rename search replacement targetSo your examples would be, respectively:
For anything more complicated such as the character range example, sed/awk might be your best bet.
(Apparently the text rendering on the site proper is different from the preview, so the straight quotes appear correctly. In the preview they looked like backticks. Hence the clarification.)
Thanks, Scotty. Very useful!
using regular expressions:
rename ‘s/prefix*/$2/’ *
it gives uninitialized error for $2 but does the work as expected.
Thank you. That sure helped this newbie.
So helpful for renaming photo files from cameras! I never knew about this useful command. Thanks for the post.