Command Line Basics: Bulk Rename Files With rename

December 18, 2009 by
Filed under: command line, HowTo, linux 

Sometimes you get a bunch of files that are named in an annoying way and you'd like to change the naming convention for all of them. Maybe you forgot to change the settings on your CD ripping software and it ended up creating a bunch of long file names that you don't like.

I recently received a bunch of files that stupidly had names starting with spaces and hyphens; ( - ). The files looked something like this:

linerd@laptop:~/demo$ ls -1
 - File number 1.txt
 - File number 2.txt
 - File number 3.txt
 - File number 4.txt
 - File number 5.txt

Starting a file name with a space is just stupid! For 5 files it's no big deal to go in and manually rename them. I had many more files, so I was in search of a tool that could change many files all at once. The aptly named rename terminal command turned out to be a great solution.

rename uses Perl expressions to recognise and replace patterns in file names. Since the expression syntax is somewhat complex, rename includes the -n parameter, (no action,) so you can try the command before executing it. To test the command to strip the " - " from the above file names, you would use:

Affiliate Link
rename -n 's/ - //' *

This gives the output

 - File number 1.txt renamed as File number 1.txt
 - File number 2.txt renamed as File number 2.txt
 - File number 3.txt renamed as File number 3.txt
 - File number 4.txt renamed as File number 4.txt
 - File number 5.txt renamed as File number 5.txt

Since the output of the test command looks good, you can make the changes to your files by re-running the command without the "-n" option.

rename 's/ - //' *

My listing of files now looks like this:

linerd@laptop:~/demo$ ls -1
File number 1.txt
File number 2.txt
File number 3.txt
File number 4.txt
File number 5.txt

Personally, I don't like spaces in my file names, so I'm going to run the command again to replace the spaces with underscores.

rename 's/ /_/g' *.txt

This command gives a result that looks like

linerd@laptop:~/demo$ ls -1
File_number_1.txt
File_number_2.txt
File_number_3.txt
File_number_4.txt
File_number_5.txt

Here's a little explanation about the above commands. The single quotes in the command are important to isolate the slashes and prevent them from being recognised as special characters by the shell. The slashes are field separators to separate the find field from the replace field. The "s" in the command stands for "substitute". The "g" that was used in the second version above stands for "global". Without it, only the first space would have been replaced with an underscore. The "*" or "*.txt" at the end of the command tells the shell which files should be affected.

Comments

5 Responses to “Command Line Basics: Bulk Rename Files With rename”

  1. Hey, this made my day! Lot easier than find exec etc.

  2. I just found that adding is actually pretty easy. I wanted to append artist name to the front of every file in a folder of music by an artist so I just did:

    rename 's//artistname-/' *.mp3

    Easier than I expected :).

  3. sean says:

    that's remove and replace characters, is there a way to add

    • Linerd says:

      @Sean - There may be a better way, but you can still add characters using the replace feature. Say you wanted to add leading zeros to the numbers in the files above.

      rename 's/r_/r_0/' *.txt
  4. Story added...

    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here:

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>