#!/bin/bash # hack this for whatever devices you card may appear on e.g. /dev/sdz9 # for my Sony Ericcson camerea phone it's important to put the device it mounts the memory card on # before the device where the phone's own memory appears #CARD_DEVS="sde1 sdf1 sdg1 sdd1 sdc1 sdb1 sda1" CARD_DEVS="memory_card_SD1 memory_card_CF1 memory_card_SM1 memory_card_MS1 sdb1 sdf1 sde1 sdg1 sdd1 sdc1 sda1 sdh1" # this is the root of photo directory tree within your home directory: this script will create subdirectories for each year and date # so on 25/12/2009 pictures will be placed in ~/pix/2009/20091225 PIX_DIR=~/pix if [ ! -d $PIX_DIR ]; then echo "*** ERROR *** no pix directory $PIX_DIR"; exit 1; fi PIX_TMP=$PIX_DIR/tmp RAW2JPG=/home/john/bin/raw2jpg EXIFTRAN=`which exiftran` EXIFTOOL=`which exiftool` SOURCE_DIR= DELETE_FROM_CARD=1 MODE=OLD usage () { cat <<_EOF_usage USAGE: `basename $0` [options] where options can be: --usage | --help | -h - this usage message --nodelete | --keep - leave images on card --source SOURCE_DIR - process images in SOURCE_DIR --rsync - experimental rsync mode _EOF_usage } if [ "$1" == "" ] ; then echo doing getpix ; else while [ "$1" != "" ] ; do #echo $1 case $1 in --usage | --help | -h) usage exit ;; --nodelete | --keep) echo "Not deleting images from card" DELETE_FROM_CARD= ;; --rsync) DELETE_FROM_CARD= MODE=RSYNC echo Experimental rsync mode ;; --source) shift if [ "$1" == "" ] ; then echo "*** ERROR *** must have source directory after --source option" ; exit 1; fi DELETE_FROM_CARD= SOURCE_DIR=$1 if [ ! -d $SOURCE_DIR ]; then echo "*** ERROR *** source $SOURCE_DIR is not a directory"; exit 1; fi echo Source: $SOURCE_DIR ;; *) echo "*** ERROR *** don't recognise option $1" usage exit ;; esac shift done fi # if [ "$DELETE_FROM_CARD" == "1" ]; then echo MOVE files from card; else echo COPY files from card; fi mk_tmp_dir () { if [ ! -d $PIX_TMP ]; then echo "Creating $PIX_TMP" if ! `mkdir $PIX_TMP`; then echo "*** ERROR *** could not create temporary directory $PIX_TMP"; return 1; fi fi } mk_dest_dir () { YYYY=`date '+%Y'` if [ ! -d $PIX_DIR/$YYYY ]; then echo "***********************"; echo " Happy New Year!"; date '+%t%Y'; echo "***********************"; fi YYYYMMDD=`date '+%Y%m%d'` PIX_DEST_DIR=$PIX_DIR/$YYYY/$YYYYMMDD if [ ! -d $PIX_DEST_DIR ]; then if ! `mkdir -pv $PIX_DEST_DIR`; then echo "*** ERROR *** could not create destination directory $PIX_DEST_DIR"; return 1; fi; fi if ! `chmod 755 $PIX_DEST_DIR`; then echo "*** WARNING *** could not chmod directory $PIX_DEST_DIR"; fi } mount_card () { echo -n "Trying " for DEV in $CARD_DEVS; do echo -n "/dev/$DEV ... " pmount /dev/$DEV 2>/dev/null && check_mount_point && find_dcim_dir && return 0 done DEV= echo "*** ERROR *** could not mount card ***" return 1 } check_mount_point () { MOUNT_POINT=/media/$DEV if [ ! -d $MOUNT_POINT ]; then echo "** /dev/$DEV mounted but not found mounted on $MOUNT_POINT" MOUNT_POINT= return 1 fi echo "mounted on $MOUNT_POINT" return 0 } find_dcim_dir () { if [ -d $MOUNT_POINT/dcim ]; then SOURCE_DIR=$MOUNT_POINT/dcim; return 0; fi if [ -d $MOUNT_POINT/DCIM ]; then SOURCE_DIR=$MOUNT_POINT/DCIM; return 0; fi echo "*** picture directory dcim or DCIM not found in $MOUNT_POINT/" return 1 } process_file () { echo "" source_file=$1 b=`basename $source_file` # lowercase filename tmpfile=`echo $b | tr '[A-Z]' '[a-z]'` echo Copying $source_file '->' $PIX_TMP/$tmpfile # if ! `cp $source_file $PIX_TMP/$tmpfile > /dev/null 2>&1`; then if ! `cp $source_file $PIX_TMP/$tmpfile`; then echo "*** ERROR *** could not copy $source_file from source to $PIX_TMP/$tmpfile" return 1 fi # try to get CreateDate from EXIF tag in file if [ "$EXIFTOOL" != "" ]; then CreateDate=`exiftool -CreateDate -S -d %Y%m%d_%H%M%S $PIX_TMP/$tmpfile | sed -e 's/CreateDate: //' -` if [ "$CreateDate" != "" ] ; then echo $tmpfile dated $CreateDate if mv $PIX_TMP/$tmpfile $PIX_TMP/${CreateDate}_${tmpfile} ; then # if file is a '.thm' then attempt to rename the corresponding '.avi' filebase=`basename $tmpfile .thm` if [ -f $PIX_DEST_DIR/${filebase}.avi ] ; then if mv $PIX_DEST_DIR/${filebase}.avi $PIX_DEST_DIR/${CreateDate}_${filebase}.avi ; then echo Rename $PIX_DEST_DIR/${filebase}.avi '->' ${CreateDate}_${filebase}.avi fi ## don't need to do anything for .mov - has exif timestamp and is dealt with as for JPGs # elif echo $tmpfile | grep \.mov$ ; then # # if this file is a '.mov' then try to rename it from the corresponding '.jpg' # for jpg in '$PIX_DEST_DIR/*_${filebase}.jpg' ; do # echo "Found .jpg ($jpg) thumbnail for ${filebase}.mov" # filebase=`basename $tmpfile .jpg` # if mv $jpg $PIX_DEST_DIR/${CreateDate}_${filebase}.mov ; then # echo Rename $jpg '->' $PIX_DEST_DIR/${CreateDate}_${filebase}.mov # fi # done fi echo Renamed $tmpfile '->' ${CreateDate}_${tmpfile} tmpfile=${CreateDate}_${tmpfile} else echo "*** ERROR ***" could not rename: $tmpfile '->' ${CreateDate}_${tmpfile} fi fi fi # is there already a file of this name in the destination directory? if [ -s $PIX_DEST_DIR/$tmpfile ] ; then echo "### $tmpfile already exists in $PIX_DEST_DIR/" # are files same? if diff $PIX_TMP/$tmpfile $PIX_DEST_DIR/$tmpfile ; then # (if true then files are same) echo "### files identical - skipping" # *DELETE NEW COPY* else # files differ echo "*** new and old versions of $tmpfile differ!!!" # leave new copy in temp dir until we can code some -suffix logic for x in 01 02 03 04 05 06 07 08 09; do echo "Checking for existing file $PIX_DEST_DIR/$tmpfile-$x" if [ ! -s $PIX_DEST_DIR/$tmpfile-$x ] ; then echo Renaming $b as $tmpfile-$x if ! mv $PIX_TMP/$tmpfile $PIX_TMP/$tmpfile-$x; then echo "*** ERROR attempting to rename $b to $tmpfile-$x" else tmpfile="$tmpfile-$x" fi break; fi done fi fi #echo "Test $b" echo Moving $tmpfile to ${PIX_DEST_DIR}/ if ! `mv $PIX_TMP/$tmpfile $PIX_DEST_DIR`; then echo "*** ERROR attempting to move $tmpfile from $PIX_TMP/ to $PIX_DEST_DIR/" return 1 fi if [ "$DELETE_FROM_CARD" == "1" ]; then echo "Deleting $source_file" rm $source_file fi case $tmpfile in *.jpg ) if [ "$EXIFTRAN" == "" ]; then echo "$0 cannot find exiftran: cannot auto-rotate image" else echo "Auto-rotate $tmpfile" $EXIFTRAN -ai $PIX_DEST_DIR/$tmpfile & fi ;; *.pef ) echo "Make JPG from .pef RAW" $RAW2JPG $PIX_DEST_DIR/$tmpfile & ;; *.cr2 ) echo "Make JPG from .cr2 RAW" $RAW2JPG $PIX_DEST_DIR/$tmpfile & ;; *) echo "(no match for post-processing)" ;; esac } process_source () { echo Processing files in picture directory $SOURCE_DIR ... for f in `find $SOURCE_DIR -type f -size +0`; do process_file $f done } mk_tmp_dir || exit 1 mk_dest_dir || exit 1 # experimental rsync-type getpix if [ "$MODE" == "RSYNC" ] ; then mount_card || exit 1 PROCESS_CARD=0 if ! rsync -av $SOURCE_DIR/ $PIX_TMP ; then echo "*** ERROR rsync-ing $SOURCE_DIR to $PIX_TMP" else echo " - rsync OK" ; SOURCE_DIR=$PIX_TMP if process_source ; then echo " - pictures from $SOURCE_DIR processed OK" rm -rf $SOURCE_DIR/* PROCESS_CARD=1 ; fi fi if pumount /dev/$DEV ; then echo "* * * * * * * * * * * * * * * * * * *" echo "* * * * OK to remove card now * * * *" echo "* * * * * * * * * * * * * * * * * * *" else echo "*** ERROR *** failed to pumount /dev/$DEV" echo "please check and unmount by hand" mount fi echo "*** Manually delete files from card" exit fi # MODE == old getpic behaviour if [ "$SOURCE_DIR" == "" ] ; then echo Source: card mount_card || exit 1 PROCESS_CARD=0 if process_source ; then PROCESS_CARD=1 ; fi if pumount /dev/$DEV ; then if [ $PROCESS_CARD == 1 ] ; then echo "* * * * * * * * * * * * * * * * * * *" echo "* * * * OK to remove card now * * * *" echo "* * * * * * * * * * * * * * * * * * *" else echo "* * * * * * * * * * * * * * * * * * * * * * *" echo "*** ERROR transferring pictures from card ***" echo "* * * * * * * * * * * * * * * * * * * * * * *" fi else echo "*** ERROR *** failed to pumount /dev/$DEV" echo "please check and unmount by hand" mount fi else # source is directory if process_source ; then echo "* OK - processed files in $SOURCE_DIR" ; fi fi ## to datestamp files in existing directory trees ## find -type d -exec exiftool '-FileName