#!/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 PIX_TMP=$PIX_DIR/tmp DELETE_FROM_CARD=1 RAW2JPG=/home/john/bin/raw2jpg EXIFTRAN=`which exiftran` EXIFTOOL=`which exiftool` if [ "$DELETE_FROM_CARD" == "1" ]; then echo MOVE files from card; else echo COPY files from card; fi if [ ! -d $PIX_DIR ]; then echo "*** ERROR *** no pix directory $PIX_DIR"; exit; fi if [ ! -d $PIX_TMP ]; then echo "Creating $PIX_TMP" if ! `mkdir $PIX_TMP`; then echo "*** ERROR *** cannot create $PIX_TMP"; exit; fi fi 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 directory $PIX_DEST_DIR"; exit; 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 && return 0 pmount /dev/$DEV 2>/dev/null && check_mount_point && find_dcim_dir && return 0 done DEV= echo "*** Couldn't 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 DCIM_DIR=$MOUNT_POINT/dcim; return 0; fi if [ -d $MOUNT_POINT/DCIM ]; then DCIM_DIR=$MOUNT_POINT/DCIM; return 0; fi echo "*** picture directory dcim or DCIM not found in $MOUNT_POINT/" return 1 } process_file () { echo "" cardfile=$1 b=`basename $cardfile` # lowercase filename tmpfile=`echo $b | tr '[A-Z]' '[a-z]'` echo Copying $cardfile '->' $PIX_TMP/$tmpfile # if ! `cp $cardfile $PIX_TMP/$tmpfile > /dev/null 2>&1`; then if ! `cp $cardfile $PIX_TMP/$tmpfile`; then echo "*** ERROR *** could not copy $cardfile from card 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 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 $cardfile" rm $cardfile 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_card () { # check_mount_point || return 0 # find_dcim_dir || return 0 echo Processing files in picture directory $DCIM_DIR ... for f in `find $DCIM_DIR -type f -size +0`; do process_file $f done } mount_card || exit 1 PROCESS_CARD=0 if process_card ; 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 ## to datestamp files in existing directory trees ## find -type d -exec exiftool '-FileName