Simplifing sorting of images with jhead
I have upgraded my camera equipment and as a part of that we have been shooting a lot more images and filling 1GB CF cards pretty quickly.
The old method of sorting our images for upload into Gallery was beginning to really slow us down. iPhoto is not an integral part of my archival method and as such it was wasted CPU cycles and overkill to manipulate the files just for upload into the main archive.
So, I needed to write a quick and dirty script to sort the images on the command line.
The script is quite straightforward. It looks in a directory, looks at the EXIF header of each file, creates a directory based upon the “File date” in the header, and copies the file there.
The pre-requisites are a shell (bash) and jhead.
In order to use the script it is basically:
chris@monolith<507>~/Desktop/temp_photos $ photo_sort.sh .
This will create a directory structure similar to this:
chris@monolith<507>~/Desktop/temp_photos $ tree -df
.
`-- ./____sorted____
`-- ./2005
|-- ./07
| |-- ./06
| |-- ./09
| |-- ./15
| |-- ./18
| |-- ./20
| |-- ./21
| |-- ./22
| |-- ./23
| `-- ./29
`-- ./08
|-- ./01
|-- ./06
`-- ./07
Here is the script:
#!/bin/bash
#
# Relabel and sort images based upon the EXIF header of the image.
#
WORKDIR="$1"
if [ "$1" = "" ]; then
echo -e "photo_sort.sh\n"
echo -e "This script sorts the contents of the passed directory passed upon"
echo -e "the \"File date\" in the EXIF tag of the image (via jhead). It "
echo -e "copies the sorted files to the wokingdir/___sorted___.\n"
exit
else
JHEAD="/usr/bin/jhead"
for image in $(ls -l "$WORKDIR" | awk '{print $9}')
do
DATESTAMP=$($JHEAD "$WORKDIR"/"$image" | grep "File date" | awk '{print $4}')
YEAR=$(echo $DATESTAMP | awk -F: '{print $1}')
MONTH=$(echo $DATESTAMP | awk -F: '{print $2}')
DAY=$(echo $DATESTAMP | awk -F: '{print $3}')
SORTOUT="$WORKDIR/____sorted____/$YEAR/$MONTH/$DAY"
if [ ! -d "$SORTOUT" ]; then
mkdir -p "$SORTOUT"
fi
cp "$WORKDIR"/$image "$SORTOUT"/.
$JHEAD -ft "$SORTOUT"/$image
done
fi
