Junk Food for the Brain

Open Source and Awesomesauce :)

How to Mass Rename Files in Linux

| Comments

Mass renaming files is no possible with the standard linux lv command, but can be made possible via a few bash tricks.

For example I have my list mp3s from Daft Punk’s Tron Legacy here:-

01-daft_punk-overture.mp3
02-daft_punk-the_grid.mp3
03-daft_punk-the_son_of_flynn.mp3
04-daft_punk-recognizer.mp3
05-daft_punk-armory.mp3
06-daft_punk-arena.mp3
07-daft_punk-rinzler.mp3
08-daft_punk-the_game_has_changed.mp3
09-daft_punk-outlands.mp3
10-daft_punk-adagio_for_tron.mp3
11-daft_punk-nocturne.mp3
12-daft_punk-end_of_line.mp3
13-daft_punk-derezzed.mp3
14-daft_punk-fall.mp3
15-daft_punk-solar_sailer.mp3
16-daft_punk-rectifier.mp3
17-daft_punk-disc_wars.mp3
18-daft_punk-c.l.u..mp3
19-daft_punk-arrival.mp3
20-daft_punk-flynn_lives.mp3
21-daft_punk-tron_legacy_(end_titles).mp3
22-daft_punk-finale.mp3

I’d like to do the following:
* Rename daft_punk to Daft Punk
* Replace the underscores with spaces

To do this, I’ll use bash to get the list of file names and pipe it through sed.

for mp3 in *; do new_name=$(echo $mp3 | sed -e 's/daft_punk/Daft Punk/g' | sed -e 's/_/ /g'); echo file $mp3 will be renamed to $new_name;done

In the previous snippet, I just echo-ed what would the files be renamed to:

$ for mp3 in *; do new_name=$(echo $mp3 | sed -e 's/daft_punk/Daft Punk/g' | sed -e 's/_/ /g'); echo file $mp3 will be renamed to $new_name;done
file 01-daft_punk-overture.mp3 will be renamed to 01-Daft Punk-overture.mp3
file 02-daft_punk-the_grid.mp3 will be renamed to 02-Daft Punk-the grid.mp3
file 03-daft_punk-the_son_of_flynn.mp3 will be renamed to 03-Daft Punk-the son of flynn.mp3
file 04-daft_punk-recognizer.mp3 will be renamed to 04-Daft Punk-recognizer.mp3
file 05-daft_punk-armory.mp3 will be renamed to 05-Daft Punk-armory.mp3
file 06-daft_punk-arena.mp3 will be renamed to 06-Daft Punk-arena.mp3
file 07-daft_punk-rinzler.mp3 will be renamed to 07-Daft Punk-rinzler.mp3
file 08-daft_punk-the_game_has_changed.mp3 will be renamed to 08-Daft Punk-the game has changed.mp3
file 09-daft_punk-outlands.mp3 will be renamed to 09-Daft Punk-outlands.mp3
file 10-daft_punk-adagio_for_tron.mp3 will be renamed to 10-Daft Punk-adagio for tron.mp3
file 11-daft_punk-nocturne.mp3 will be renamed to 11-Daft Punk-nocturne.mp3
file 12-daft_punk-end_of_line.mp3 will be renamed to 12-Daft Punk-end of line.mp3
file 13-daft_punk-derezzed.mp3 will be renamed to 13-Daft Punk-derezzed.mp3
file 14-daft_punk-fall.mp3 will be renamed to 14-Daft Punk-fall.mp3
file 15-daft_punk-solar_sailer.mp3 will be renamed to 15-Daft Punk-solar sailer.mp3
file 16-daft_punk-rectifier.mp3 will be renamed to 16-Daft Punk-rectifier.mp3
file 17-daft_punk-disc_wars.mp3 will be renamed to 17-Daft Punk-disc wars.mp3
file 18-daft_punk-c.l.u..mp3 will be renamed to 18-Daft Punk-c.l.u..mp3
file 19-daft_punk-arrival.mp3 will be renamed to 19-Daft Punk-arrival.mp3
file 20-daft_punk-flynn_lives.mp3 will be renamed to 20-Daft Punk-flynn lives.mp3
file 21-daft_punk-tron_legacy_(end_titles).mp3 will be renamed to 21-Daft Punk-tron legacy (end titles).mp3
file 22-daft_punk-finale.mp3 will be renamed to 22-Daft Punk-finale.mp3

I’ll walk through each snippet before we go any further:

for mp3 in *;

the beginning of the bash for loop. Assigns the variable mp3 to every file in the current directory.

do new_name=$(echo $mp3 | sed -e 's/daft_punk/Daft Punk/g' | sed -e 's/_/ /g');    

Assigns the new_name variable to the result of the 2 sed operations on the text of the file name.

Now that new_variable contains the file name that we wish to be renamed, we can use bash substitution can create a proper mv command.

$ mv -v $mp3 "$new_name"

So the full command would look like this:

$ for mp3 in *; do new_name=$(echo $mp3 | sed -e 's/daft_punk/Daft Punk/g' | sed -e 's/_/ /g'); echo file $mp3 will be renamed to $new_name;mv -v $mp3 "$new_name"; done

The final output would look like this:-

file 01-daft_punk-overture.mp3 will be renamed to 01-Daft Punk-overture.mp3
`01-daft_punk-overture.mp3' -> `01-Daft Punk-overture.mp3'
file 02-daft_punk-the_grid.mp3 will be renamed to 02-Daft Punk-the grid.mp3
`02-daft_punk-the_grid.mp3' -> `02-Daft Punk-the grid.mp3'
file 03-daft_punk-the_son_of_flynn.mp3 will be renamed to 03-Daft Punk-the son of flynn.mp3
`03-daft_punk-the_son_of_flynn.mp3' -> `03-Daft Punk-the son of flynn.mp3'
file 04-daft_punk-recognizer.mp3 will be renamed to 04-Daft Punk-recognizer.mp3
`04-daft_punk-recognizer.mp3' -> `04-Daft Punk-recognizer.mp3'
file 05-daft_punk-armory.mp3 will be renamed to 05-Daft Punk-armory.mp3
`05-daft_punk-armory.mp3' -> `05-Daft Punk-armory.mp3'
file 06-daft_punk-arena.mp3 will be renamed to 06-Daft Punk-arena.mp3
`06-daft_punk-arena.mp3' -> `06-Daft Punk-arena.mp3'
file 07-daft_punk-rinzler.mp3 will be renamed to 07-Daft Punk-rinzler.mp3
`07-daft_punk-rinzler.mp3' -> `07-Daft Punk-rinzler.mp3'
file 08-daft_punk-the_game_has_changed.mp3 will be renamed to 08-Daft Punk-the game has changed.mp3
`08-daft_punk-the_game_has_changed.mp3' -> `08-Daft Punk-the game has changed.mp3'
file 09-daft_punk-outlands.mp3 will be renamed to 09-Daft Punk-outlands.mp3
`09-daft_punk-outlands.mp3' -> `09-Daft Punk-outlands.mp3'
file 10-daft_punk-adagio_for_tron.mp3 will be renamed to 10-Daft Punk-adagio for tron.mp3
`10-daft_punk-adagio_for_tron.mp3' -> `10-Daft Punk-adagio for tron.mp3'
file 11-daft_punk-nocturne.mp3 will be renamed to 11-Daft Punk-nocturne.mp3
`11-daft_punk-nocturne.mp3' -> `11-Daft Punk-nocturne.mp3'
file 12-daft_punk-end_of_line.mp3 will be renamed to 12-Daft Punk-end of line.mp3
`12-daft_punk-end_of_line.mp3' -> `12-Daft Punk-end of line.mp3'
file 13-daft_punk-derezzed.mp3 will be renamed to 13-Daft Punk-derezzed.mp3
`13-daft_punk-derezzed.mp3' -> `13-Daft Punk-derezzed.mp3'
file 14-daft_punk-fall.mp3 will be renamed to 14-Daft Punk-fall.mp3
`14-daft_punk-fall.mp3' -> `14-Daft Punk-fall.mp3'
file 15-daft_punk-solar_sailer.mp3 will be renamed to 15-Daft Punk-solar sailer.mp3
`15-daft_punk-solar_sailer.mp3' -> `15-Daft Punk-solar sailer.mp3'
file 16-daft_punk-rectifier.mp3 will be renamed to 16-Daft Punk-rectifier.mp3
`16-daft_punk-rectifier.mp3' -> `16-Daft Punk-rectifier.mp3'
file 17-daft_punk-disc_wars.mp3 will be renamed to 17-Daft Punk-disc wars.mp3
`17-daft_punk-disc_wars.mp3' -> `17-Daft Punk-disc wars.mp3'
file 18-daft_punk-c.l.u..mp3 will be renamed to 18-Daft Punk-c.l.u..mp3
`18-daft_punk-c.l.u..mp3' -> `18-Daft Punk-c.l.u..mp3'
file 19-daft_punk-arrival.mp3 will be renamed to 19-Daft Punk-arrival.mp3
`19-daft_punk-arrival.mp3' -> `19-Daft Punk-arrival.mp3'
file 20-daft_punk-flynn_lives.mp3 will be renamed to 20-Daft Punk-flynn lives.mp3
`20-daft_punk-flynn_lives.mp3' -> `20-Daft Punk-flynn lives.mp3'
file 21-daft_punk-tron_legacy_(end_titles).mp3 will be renamed to 21-Daft Punk-tron legacy (end titles).mp3
`21-daft_punk-tron_legacy_(end_titles).mp3' -> `21-Daft Punk-tron legacy (end titles).mp3'
file 22-daft_punk-finale.mp3 will be renamed to 22-Daft Punk-finale.mp3
`22-daft_punk-finale.mp3' -> `22-Daft Punk-finale.mp3'

A ls -l just to prove the files:-

$ ls 
01-Daft Punk-overture.mp3
02-Daft Punk-the grid.mp3
03-Daft Punk-the son of flynn.mp3
04-Daft Punk-recognizer.mp3
05-Daft Punk-armory.mp3
06-Daft Punk-arena.mp3
07-Daft Punk-rinzler.mp3
08-Daft Punk-the game has changed.mp3
09-Daft Punk-outlands.mp3
10-Daft Punk-adagio for tron.mp3
11-Daft Punk-nocturne.mp3
12-Daft Punk-end of line.mp3
13-Daft Punk-derezzed.mp3
14-Daft Punk-fall.mp3
15-Daft Punk-solar sailer.mp3
16-Daft Punk-rectifier.mp3
17-Daft Punk-disc wars.mp3
18-Daft Punk-c.l.u..mp3
19-Daft Punk-arrival.mp3
20-Daft Punk-flynn lives.mp3
21-Daft Punk-tron legacy (end titles).mp3
22-Daft Punk-finale.mp3