Pattern based rename of files with spaced names

My amnesiac 350d lost its date as usual and I accidentally set the date to 2012 instead of 2013, making the EXIF data and filenames all wrong when importing them through Dropbox’s file import.

Rather than going through and doing it one by one I figured scripting it would work. One-liners were breaking bash’s ability to play nice so I had to make a little file, and here it is.

#!/bin/bash
for i in 2012*
  do
    x=`echo $i | sed 's/ /\\\ /g'
    echo $x `echo $x | sed 's/2012/2013/g'` | xargs -n 2 mv
  done

Basically:

  1. For every filename starting with 2012
  2. Escape the spaces from the filename and store it in a variable.
  3. Output the escaped original filename then change 2012 to 2013 and make that the destination.
  4. Pipe this into xargs with a -n 2 filter (making every line have two arguments) and run mv on the arguments.

Or even shorter, “_move (filename) (filename with 2012 replaced)_” for each file.

This blog post helped a bit!



#bash #command line #OSX #scripting #sed