Skip to content

Instantly share code, notes, and snippets.

@ldong
Forked from jvhaarst/movedigiphotos.bash
Created December 27, 2015 17:58
Show Gist options
  • Save ldong/42f2e2f79ca233be5402 to your computer and use it in GitHub Desktop.
Save ldong/42f2e2f79ca233be5402 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.
set -o nounset
#set -o errexit
BASEDIR=`pwd`
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for file in `find "$BASEDIR" -maxdepth 1 -iname "*.JPG" -or -iname "*.CRW" -or -iname "*.THM" -or -iname "*.NEF"`
do
x=`basename ${file}`
DATE=`exiftool -q -t -DateTimeOriginal "${x}" | awk -F '\t' '{print $2}' | grep -o '[[:digit:]]\{4\}:[[:digit:]]\+:[[:digit:]]\+'`
if [ ! -z "$DATE" ];
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
#INSTDIR=${BASEDIR}/${YEAR}/${YEAR}-${MONTH}/${YEAR}-${MONTH}-${DAY}
INSTDIR=${BASEDIR}/${YEAR}_${MONTH}_${DAY}
INSTFILE="${INSTDIR}/${x}"
if [ ! -d "${INSTDIR}" ]; then
mkdir -p "${INSTDIR}"
fi
if [ -f "${INSTFILE}" ] && ! cmp -s "${file}" "${INSTFILE}"
then
echo "WARNING: ${INSTFILE} already exists and is different from ${x}."
else
echo "Moving '$x'"
cp -p "${file}" "${INSTFILE}"
if ! cmp -s "${file}" "${INSTFILE}"
then
echo "WARNING: copying failed somehow, will not delete original '${x}'"
else
rm -f "${file}"
fi
fi
else
echo "WARNING: '${x}' doesn't contain date."
fi
else
echo "WARNING: '${x}' doesn't contain date."
fi
done
for file in `find "$BASEDIR" -maxdepth 1 -iname "*.JPG" -or -iname "*.CRW" -or -iname "*.THM" -or -iname "*.NEF" -or -iname "*avi" -or -iname "*mov"`
do
x=`basename ${file}`
FILE_DATE=`stat -f "%Sm" -t %F $file | awk '{print $1}'| sed 's/-/_/g'`
INSTDIR=${BASEDIR}/${FILE_DATE}
INSTFILE="${INSTDIR}/${x}"
if [ ! -d "${INSTDIR}" ]; then
mkdir -p "${INSTDIR}"
fi
if [ -f "${INSTFILE}" ] && ! cmp -s "${file}" "${INSTFILE}"
then
echo "WARNING: ${INSTFILE} already exists and is different from ${x}."
else
echo "Moving '${x}'"
cp -p "${file}" "${INSTFILE}"
if ! cmp -s "${file}" "${INSTFILE}"
then
echo "WARNING: copying failed somehow, will not delete original '${x}'"
else
rm -f "${file}"
fi
fi
done
IFS=$SAVEIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment