Monday, May 17, 2010

Rename Digital Camera files with the Date and Time using a 7 line KSH script

I hate the way digital cameras name files. Both the Sony and Casio cameras we have owned use an incremental file naming convention. This name is not connected to the date and time of the photo.

When I want to collect photos from a particular date of the year, ( e.g for my son or daughters birthdays) its pretty labour intensive to do this manually.

Now I have got a Unix based Mac I can revert to a scripting language I learnt many years ago to do the job for me. (Well using iPhoto is NOT the point :) and other examples are out there for that particular program.)

I DO not use on this on the original files, but copy into another directory, here $HOME/Documents/pictures/

The 7 Line Script
ls *JPG|while read i
do
echo $i
d=`strings $i |egrep "[1-2][0-9][0-9][0-9]:"|head -1|sed "s/ /-/g
s#:#-#g"`
cp $i $HOME/Documents/pictures/$d.jpg
done

*The strings bit converts the binary file into a readable file for the egrep and sed commands.

Running the strings command on a photo file on its own.
2009:10:13 19:05:29
2009:10:13 19:05:29
2009:10:13 19:05:29


We dont use any cyber shot - mult shot camera function so I can safely use the time to the second for the filename.

Then a simple ls command to list the dates for the yearly events.

ls $HOME/Documents/pictures/20[0-9][0-9]-10-13* - every 13th October for the year 2000 onwards.

Then copy them into a temporary directory with a simple cp after creating the temp directory.

cp $HOME/Documents/pictures/20[0-9][0-9]-10-13* $HOME/temp

Google +