Remove underscores from filenames and convert to proper case

I was converting a bunch of CDs to MP3s the other day and after doing around 10 CDs I decided to change the naming convention of the resulting MP3s. The original format was to be all lower case and to replace spaces with underscores:

are_you_the_one_that_ive_been_waiting_for.mp3     <---- By Nick Cave if you were interested :-)

Using alt+c on the command line to capitalise the first letter of each word is a great help, but then there is the required action of replacing "_" with "\ ". Not what I would call fun when there are 10 CDs worth of mp3s to rename.

After some playing around I came up with the following script that will rename the above MP3 (and every mp3 and directory within the current directory, whilst ignoring m3u files) to:

Are You The One That Ive Been Waiting For.mp3

If you have alterations you would like to make then please make them and post them here :-)

--- cut --- 

for i in ./* ; do
        if [[ `echo "$i" | grep -r '_' | grep -v 'm3u'` ]]
        #       mv "$i" "`echo "$i" | sed 's/_/\ /g' | perl -p -e 's/(\\w+)/\\u\\L$1/g;' | sed 's/Mp3/mp3/g'`";
                echo "`echo "$i" | sed 's/_/\ /g' | perl -p -e 's/(\\w+)/\\u\\L$1/g;' | sed 's/Mp3/mp3/g'`";

---- cut ---- 

This script will only echo out the command and not actual run. Uncomment the commented line and comment out the line below will result in files being renamed.

NOTE: The perl section is borrowed from some web site. I was going to use sed again, but sed is a pretty cruddy choice for the proper capitalisation function.

Regards, Bawdo2001

tags: bash, Linux

Mon 30 Jan 2006, 16:41


This was so incredibly helpful. Thank you so much