-   Linux - General (
-   -   awk cli to rename a list of files... (

pld 02-15-2005 04:53 PM

awk cli to rename a list of files...
Hi all,

Searched and found a couple of similar posts, but nothing with just what I am looking for...

I have a directory of music formatted as: track - artist - trackname.mp3

I want to reorder this data to be artist-album-track-trackname.mp3

The thing is, not all of the filenames are consistent with formatting. So i didnt want to write a shell script per se, because i also thought it might be nice to learn awk a little better, and I think i am getting the hang of it, sorta.

Now, I can do:

ls | awk -F' - ' '{print "mv "$0" "$2-$1-$3}'

This outputs just what I want, but i cannot seem to get the resulting string to execute on my machine. I tried piping it to sh as:

ls | awk -F' - ' '{print "mv "$0" "$2-$1-$3}' | sh

but i get an error regarding mv trying to rename multiple files...

[pld@mother 311_tmp]$ ls | awk -F' - ' '{print "mv "$0" "$0".old"}' | sh
mv: when moving multiple files, last argument must be a directory

So, how exactly do i get my nicely newly formatted string to execute?

tia, (once again.. and again.. and again..)

homey 02-15-2005 06:26 PM


I want to reorder this data to be artist-album-track-trackname.mp3
You don't show where "album" came from on the original names so I left that out....


cd /home/images

for i in *.mp3 ; do
  j=`echo $i | awk -F- '{print$2"-"$1"-"$3}'`
  mv "$i" "$j"

pld 02-15-2005 07:15 PM

Thanks homey, but perhaps I should have been a little more clear in my request.

is there any way to do this on the cli w/o having to write a shell script?

the awk statement in my original post works fantastic, and i can get the string out just how it should appear if i manually typed the command, but then i dont know how to actually execute the string...

the reason for doing this on the cli is that the file formatting is not consistent throughout the directories, and may require some simple manual adjustment of the script on the fly...

am i really stuck making a shell script?

homey 02-15-2005 07:38 PM

Yes, you can put this on a single command line. Just needs semi-colons...

for i in *.mp3 ; do  j=`echo $i | awk -F- '{print$2"-"$1"-"$3}'` ;  mv "$i" "$j" ; done

pld 02-15-2005 10:57 PM

of course!
figures... i just didnt think of doing it like that (smatterings of an obfuscated c code from long ago come rushing back).

Thanks a bunch, i really appreciate it!

All times are GMT -5. The time now is 07:21 PM.