Looking for a script please (I think)

Nils Kassube kassube at gmx.net
Sat Jun 2 22:26:01 UTC 2007


Bob Adams wrote:
> Can anybody suggest a terminal script (or any other way) to do the
> following please?
>
> I have about 500 mp3 music files in a single directory which are
> currently named as eg:
>
> "Supremes - Baby Love.mp3" etc, etc
>
> How can I reverse the naming convention to say;
>
> "Baby Love - Supremes.mp3" and for every other file as well
>
> Is it possible, easy, hard, impossible? All suggestions gratefully
> received.

If the delimiter between interpret and title is " - " and this is nowhere 
else in the filename, this script should to the trick:

#/bin/sh
for f in *;do
 i=${f% - *}
 t=${f#* - }
 t=${t%.mp3}
 n="$t - $i.mp3"
 mv "$f" "$n"
done

Disclaimer: I can't guarantee that nothing bad happens to your files 
because this isn't well tested. Therefore, you may want to check if 
everything will work before you actually rename the files. Then you could 
try this test version first:

#/bin/sh
for f in *;do
 i=${f% - *}
 t=${f#* - }
 t=${t%.mp3}
 n="$t - $i.mp3"
 echo "$f -> $n" >> rename.txt
done

The output is in rename.txt and each line should look like this (from your 
example):

Supremes - Baby Love.mp3 -> Baby Love - Supremes.mp3


Nils




More information about the ubuntu-users mailing list