Batch Rename Files with rename

By | 2011/04/24

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/' *


Fedora users, try something like this:

$ rename abcd wxyz *


abcd_1-2011.txt -> wxyz_1-2011.txt

8 thoughts on “Batch Rename Files with rename

  1. Jigar Jamnapara

    Very Helpful this site for me..


  2. Dawit

    I’m definitely ‘geeking out’ here! I put the commands to work and they put a smile on my face! Thanks Scott!

  3. Mikkel

    This seems to have an even simpler syntax on my CentOS 5.5 server:

    rename search replacement target

    So your examples would be, respectively:

    rename .jpeg .jpg *.jpeg
    [don’t think the pseudo-regexp is possible]
    rename ' ' _ * [those are straight quotes, of course]
    rename abcd wxyz *
    rename DSCN party *

    For anything more complicated such as the character range example, sed/awk might be your best bet.

    1. Mikkel

      (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.)

  4. Fatih P

    using regular expressions:

    rename ‘s/prefix*/$2/’ *

    it gives uninitialized error for $2 but does the work as expected.

  5. Ken Smith

    So helpful for renaming photo files from cameras! I never knew about this useful command. Thanks for the post.


Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.