Rename Files in Bulk from the Command Line

Posted by gmendoza on July 6, 2008 under Tech Tips | 5 Comments to Read

Renaming a large number of files can seem like a daunting task, but no worries, your trusty Linux CLI is at your service. For this example, we will rename a number of MP3’s located in multiple subdirectories with a couple very easy commands; “find” and “rename”.

By listing the following directory, you’ll see that the MP3’s have been named with “(LP Version)”, and of course I don’t like this naming convention.

cd ~/Music/Metallica/Metallica/
ls -1

01 - Enter Sandman (LP Version).mp3
02 - Sad But True (LP Version).mp3
03 - Holier Than Thou (LP Version).mp3
04 - The Unforgiven (LP Version).mp3
05 - Wherever I May Roam (LP Version).mp3
06 - Don't Tread On Me (LP Version).mp3
07 - Through The Never (LP Version).mp3
08 - Nothing Else Matters (LP Version).mp3
09 - Of Wolf And Man (LP Version).mp3
10 - The God That Failed (LP Version).mp3
11 - My Friend Of Misery (LP Version).mp3
12 - The Struggle Within (LP Version).mp3

We’ll use the “rename” command to search for and delete the string ” (LP Version)” in any of the mp3 file names.

Syntax:

rename (search command) (files)
rename 's/search_for_string/replace_string_with_this/' files

To delete the matching string, simply leave the replace area empty like so:

rename 's/search_for_string//' files

Our Example:

rename 's/ \(LP Version\)//' *.mp3

Notice, the left and right parentheses need to be preceded with a backslash “\” character, although the spaces do not. The backslash is a metacharacter used to give you control over what your are matching against. For more info, here’s a link to a decent tutorial on the matter.

You can see the results of the command below.

ls -1
01 - Enter Sandman.mp3
02 - Sad But True.mp3
03 - Holier Than Thou.mp3
04 - The Unforgiven.mp3
05 - Wherever I May Roam.mp3
06 - Don't Tread On Me.mp3
07 - Through The Never.mp3
08 - Nothing Else Matters.mp3
09 - Of Wolf And Man.mp3
10 - The God That Failed.mp3
11 - My Friend Of Misery.mp3
12 - The Struggle Within.mp3

Now, to rename a large number of files spanning multiple directories, simply combine “rename” with the power of the “find” command.

Syntax:

find . -type f -name *.mp3 -exec rename 's/ \(LP Version\)//' '{}' \;

In this example, we searched starting from the current directory for only files with .mp3 in their file names. We use the find command’s -exec option to execute the rename command against the result set. See the find(1) manpage for more info.

Other useful examples:

Replace all spaces with underscores.
rename 's/ /\_/g' *.mp3

Replace all uppercase with lowercase characters
rename 'y/[A-Z]/[a-z]/' *.mp3

Easy stuff, and you don’t even need any fancy GUI applications to do the job!

Share and Enjoy:
  • Print
  • Facebook
  • Twitter
  • del.icio.us
  • RSS
  • Digg
  • email
  • LinkedIn
  • Identi.ca
  • Google Bookmarks
  • StumbleUpon

Related posts:

  1. Vim in Color
  2. RAR and UNRAR from Linux CLI
  3. Line wrapping text made easy with fold
  4. Split and Reassemble Files
  5. Quickly Identify Video File Attributes

Comments

  • Bill said,

    I’m trying to do this, I have “Discogs.url.url” files in about 600 different directories and I want to drop the last “.url”. Here’s the syntax I used and the message I get

    /media/sdb1/Music/mp3/electronic$

    find . -type f -name Discogs.url.url -exec rename ’s/ Discogs.url//’ ‘{}’ ;

    find: missing argument to `-exec’

    Can you perhaps tell me what I did wrong?

  • Bill said,

    Hm okay… i noticed a few problems above. However, this is my new command and it doesn’t do anything

    find /media/sdb1/Music/mp3/electronic* -type f -name Discogs.url.url -exec rename ’s/ Discogs.url//’ ‘{}’ \;

    see, i want to take all the Discogs.url.url files in the directory electronic and rename them to Discogs.url

    Thanks.

  • gmendoza said,

    Ack! wait up. You have a space in front of your search and replace… which is why nothing is happening. But you also risk deleting the wrong part of your file names.

    ’s/ Discogs.url//’ will end up deleting anything that matches ” Discogs.url”. That will leave you with a file name of just “.url”. hehe.

    You probably want the following:

    cd /media/sdb1/Music/mp3/electronic
    find . -type f -name Discogs.url.url -exec rename ’s/Discogs.url/Discogs/’ ‘{}’ \;

    This will change the “Discogs.url” to simply “Discogs”, making the new full file name “Discogs.url”

  • Bill said,

    Ah. I see. I manually renamed the first 300 files and then got sick of it and found your blog. i think i get the general idea, though i’m trying to figure out the syntax of all the elements. Thank you very much. This will be very helpful in the future.

  • caridreni said,

    hm. bookmarked :)

Add A Comment