Some days back I got a collection of 100 Greatest Piano Works cd collection. The first thing I did was to rip the CD in flac and preserve a lossless copy of the disks. Then I converted those to ogg files (with oggenc ) so that I could take it in my iPod and listen at any time. The problem occurred at the final moment when I realized that the file names were really long and which would be very difficult to scan through my iPod (with Rockbox). The file name was in this format : %n – %a – %t.ogg  for example : For example :
“17 – Robert Schumann (1810-1856) – THE DAVIDSBÜNDLER DANCES, 18 CHARACTERISTIC PIECES, OP. 6: XIV. Zart und singend – Dolce e cantando.ogg” . The western classical musics this combination is very long most almost all the tracks, and these files with huge file names and lot of files having ‘:’ in the name being refused to copied directly. So I needed to truncate or rename the 100 files in a way so that they are identifiable and also truncated to short names.
I decided to keep only the track name and composers names (%n – %a fields), by renaming the files with some batch file renaming process.
The new name generations would work fine with simple cut with a delimiter character ‘-‘ and cutting the first two fields, but it failed as the artist names had the birth and death years written with a ‘-‘ separating them. So this failed.
I was almost on my way to write a application in C with taglib API to completely dedicated to this purpose. Writing such static apps for only one purpose is not a good idea.
Going into #bash in freenode IRC gave me the idea to use awk , where i could use much more complex field separators.
The good luck was that the field was separated by ‘ – ‘ , a hyphen surrounded by two blank spaces. And the birth death year pair was separated by ‘-‘ surrounded with no blank spaces. So this showed the way to write a shell script with an integrated awk cutting out fields.
I am presenting the script which made a smooth copy from my disk to my iPod with on-the-fly rename. (I first tried touch ing the newly created names to check if it really works, or do something horrible.)


for name in "$src"*.ogg
  new_name=$(echo "$name" | awk -F ' - ' '{ print $1" - "$2".ogg" }')
  cp -v "$name" "$dest/$(basename "$new_name")"

This would accept a source directory and a destination directory (no checking is done). The script would find all .ogg files in the source directory and then rename the files in the given format and make the destination directory by appending the basename of the source file name path to the destination directory and copy from the source to the generated destination name. The awk cuts out the 1th and 2nd fields , where the field seperator is ‘ – ‘ (a hyphen surrounded by blank spaces.
the script above would generate such output:
“17 – Robert Schumann (1810-1856) – THE DAVIDSBÜNDLER DANCES, 18 CHARACTERISTIC PIECES, OP. 6: XIV. Zart und singend – Dolce e cantando.ogg”

would be converted into

“17 – Robert Schumann (1810-1856).ogg”
single files could be handled without this script simply by piping the file name in to input of the awk script and using the output as the destination name, probably with some other path combination.
I would ask the readers to comment on this post if they find something objectionable or incorrect in the script, or addition of something which would avoid any failure in any special cases.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s