One minor problem with my Motorola Razr is that the filenames of downloaded images are munged into a somewhat stupid ‘DD-MM-YYY_xxxx.jpg’ format. And as dates are not preserved when I load the files across into my Mac, I wind up with a bunch of stupidly named and largely unsortable files.
Ok, this is a job for a command on UNIX (Mac OS X in this case) called AWK. AWK is one of the most venerable of the UNIX command-line utilities. Wikipedia has a page on AWK here.
The easiest way to describe AWK is that it allows you to apply a condition to a line of input, and to then ‘do something’ with lines of input that meet the condition, typically printing a new format, or building a command and piping to some other program.
The following piece of AWK applies all three of these common behaviors to do the following:
- Pick up a filename
- Ignore the filename if it does not start with two digits
- Reformat the filename into a simple ‘mv’ command
- Pipe the ‘mv’ command to a cshell
$ ls | awk 'substr($0,1,2)+0 > 0{print"mv "$0" 20"substr($0,7,2)"-"substr($0,4,2)"-"substr($0,1,2)"_"substr($0,10,4)".jpg"}' | csh
In this case, we transform ‘dd-mm-yy_xxxx.jpg’ to ‘mv dd-mm-yy_xxxx.jpg yyyy-mm-dd_xxxx.jpg’ and execute the commands in a csh.
The funny looking part at the start is the condition, which simply tests that substr($0,1,2) added to 0 is a number > 0.
Have fun with AWK.
