Skip to content

Instantly share code, notes, and snippets.

@sko-lv
Forked from jvhaarst/movedigiphotos.bash
Last active June 28, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sko-lv/91ae5b7dbf40a83df536b9d53d12804c to your computer and use it in GitHub Desktop.
Save sko-lv/91ae5b7dbf40a83df536b9d53d12804c to your computer and use it in GitHub Desktop.
Bash script to move images, based on exif data and file timestamp
#!/bin/bash
# Reads EXIF creation date from all .JPG files in the
# current direcotry and moves them carefully under
#
# $BASEDIR/YYYY/YYYY-MM/YYYY-MM-DD/
#
# ...where 'carefully' means that it does not overwrite
# differing files if they already exist and will not delete
# the original file if copying fails for some reason.
#
# It DOES overwrite identical files in the destination directory
# with the ones in current, however.
#
# This script was originally written and put into
# Public Domain by Jarno Elonen <elonen@iki.fi> in June 2003.
# Feel free to do whatever you like with it.
# Defaults
TOOLS=(exiftool jq) # Also change settings below if changing this, the output should be in the format YYYY:MM:DD
DEFAULTDIR='/Users/jvhaarst/Pictures/van camera/van camera'
# activate debugging from here
#set -o xtrace
#set -o verbose
# Improve error handling
set -o errexit
set -o pipefail
# Check for executable existence
for TOOL in ${TOOLS[*]}
do
hash $TOOL 2>/dev/null || { echo >&2 "I require $TOOL but it's not installed. Aborting."; exit 1; }
done
# Enable handling of filenames with spaces:
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
# Use BASEDIR from commandline, or default if none given
BASEDIR=${1:-$DEFAULTDIR}
for FILE in `find . -maxdepth 1 -iname "*.JPG" -or -iname "*.JPEG" -or -iname "*.CRW" -or -iname "*.THM" -or -iname "*.RW2" -or -iname "*AVI" -or -iname "*MOV" -or -iname "*mp4" -or -iname "*PNG"`
do
INPUT=`basename "${FILE}"`
DATE=$(${TOOLS[0]} -q -t -dateformat "%Y:%m:%d" -j -CreateDate "${INPUT}" | ${TOOLS[1]} --raw-output '.[].CreateDate')
if [ -z "$DATE" ] # If exif extraction failed
then
DATE=`stat -f "%Sm" -t %F "${INPUT}" | awk '{print $1}'| sed 's/-/:/g'`
fi
if [ ! -z "$DATE" ]; # Doublecheck
then
YEAR=`echo $DATE | sed -E "s/([0-9]*):([0-9]*):([0-9]*)/\\1/"`
MONTH=`echo $DATE | sed -E "s/([0-9]*):([0-9]*):([0-9]*)/\\2/"`
DAY=`echo $DATE | sed -E "s/([0-9]*):([0-9]*):([0-9]*)/\\3/"`
if [ "$YEAR" -gt 0 ] & [ "$MONTH" -gt 0 ] & [ "$DAY" -gt 0 ]
then
OUTPUT_DIRECTORY=${BASEDIR}/${YEAR}_${MONTH}_${DAY}
mkdir -pv ${OUTPUT_DIRECTORY}
OUTPUT=${OUTPUT_DIRECTORY}/${INPUT}
if [ -e "$OUTPUT" ] && ! cmp -s "$INPUT" "$OUTPUT"
then
echo "WARNING: '$OUTPUT' exists already and is different from '$INPUT'."
else
echo "Moving '$INPUT' to $OUTPUT"
rsync -ah --progress "$INPUT" "$OUTPUT"
if ! cmp -s "$INPUT" "$OUTPUT"
then
echo "WARNING: copying failed somehow, will not delete original '$INPUT'"
else
rm -f "$INPUT"
fi
fi
else
echo "WARNING: '$INPUT' doesn't contain date."
fi
else
echo "WARNING: '$INPUT' doesn't contain date."
fi
done
# restore $IFS
IFS=$SAVEIFS
@sko-lv
Copy link
Author

sko-lv commented Jun 28, 2016

Corrected wrong directory name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment