Unix I miss you

november 11, 2008

A few weeks ago I removed the DRM from my iTunes music collection, but I did’t really do anything else about it. Now I want to move my DRM free music somewhere else (the .m4a files), but not the .m4p file. Of cause I didn’t want to manually find every single .m4a fil, that would take to long. Solution: Install Cygwin on the WinXP VmWare installation.

Now that I have my Unix tool chest, I miss Unix, all I need to do is find the right tool for the job. I want to find a bunch of files so… I’ll use “find”.

The following command will find all my .m4a files:
find -name "*.m4a"

Good, now we have a list of files. I want to move all these file to an identical directory structure somewhere else, for this I need to get the directory part of the find result and create that directory elsewhere and move the file to that location afterwards.


find -name *.m4a -fprintf move.sh 'mkdir -p /music/%h && mv %h/%f /music/%h/\n'

This will make find create a small script for me. The -fprint option will write the output to a file, I called it move.sh. The part after the move.sh tells find how to format the output. The %h tells find that I want the directory part of the result here, and %f is the filename.

The “mkdir -p” is something that many doesn’t know about, but what it does is to recursively create a file path. This means that we can create /foo/bar/zoo in a one-line, rather than first create /foo, the /foo/bar and lastly /foo/bar/zoo. Of cause mv just moves the file, the & tells bash online to run the mv command if the previous command was successful, mkdir managed to create the directory.

Lastly I just need to run:
bash move.sh
to move all my files.

Aaah, a nice bit of Unix loving in a world of to much Windows.

Follow

Get every new post delivered to your Inbox.