Skip to content

Instantly share code, notes, and snippets.

@ncoevoet
Last active March 6, 2023 19:27
Show Gist options
  • Save ncoevoet/5b321f63cc3cb125b9aa1156335d5bf7 to your computer and use it in GitHub Desktop.
Save ncoevoet/5b321f63cc3cb125b9aa1156335d5bf7 to your computer and use it in GitHub Desktop.
Order your photos from your usb card to your hard drive, ordered by years/month, no risk of override or accidental deletion with md5sum
#!/bin/bash
# Author : Nicolas Coevoet
# This script read exifs informations of files and copy them to output/YEAR/MONTH/yearmonthday_hourminutesecond.ext
# It checks md5sum in case file with the same name is found and skip it md5sum matchs or create a new one with _<digit> if different.
# Dependencies are exiftool exiv2
if [ ! -n "${1}" ]; then
echo "Usage : ./{0} src/ output/"
exit 1
fi
if [ ! -n "${2}" ]; then
echo "Usage : ./{0} ${1}/ output/"
exit 1
fi
if [ ! "${3}" = "run" ]; then
echo "${0} run"
find "${1}" -type f -exec "${0}" {} "${2}" "run" \;
echo "Done."
else
#echo "${1}"
extension=${1##*.}
extension=$(echo "${extension}" | tr 'A-Z' 'a-z')
time=`exiftool -DateTimeOriginal -fast -s -s -s -q "${1}" 2>/dev/null`
nb=$((`echo $time | sed 's/[^:]//g' | wc -m`-1))
if [ ! $nb -ne 0 ]; then
echo " ${1} Ignored - no exif data"
else
full=`echo ${time} | cut -d' ' -f1`
year=`echo ${full} | cut -d':' -f1`
month=`echo ${full} | cut -d':' -f2`
day=`echo ${full} | cut -d':' -f3`
local=`echo ${time} | cut -d' ' -f2`
hour=`echo ${local} | cut -d':' -f1`
min=`echo ${local} | cut -d':' -f2`
second=`echo ${local} | cut -d':' -f3`
dir="${2}${year}/${month}/" #here you can specify ${year}/${month}/${day}/ if you want
mkdir -p "${dir}"
copied=0
count=0
filename="${year}${month}${day}_${hour}${min}${second}"
file="${dir}${filename}"
skipped=0
if [ -s "${dir}${filename}.${extension}" ]; then
sumSrc=`md5sum "${1}" | cut -d' ' -f1`
sumOut=`md5sum "${dir}${filename}.${extension}" | cut -d' ' -f1`
if [ "${sumSrc}" = "${sumOut}" ]; then
echo "${1} Skipped"
skipped=1
else
while [ $copied -eq 0 ];do
if [ -s "${dir}${filename}_${count}.${extension}" ]; then
sumOut=`md5sum "${dir}${filename}_${count}.${extension}" | cut -d' ' -f1`
if [ "${sumSrc}" = "${sumOut}" ]; then
echo "${1} Skipped"
copied=1
skipped=1
else
count=$(expr $count + 1)
fi
else
cp -a "${1}" "${dir}${filename}_${count}.${extension}"
file="${dir}${filename}_${count}"
echo "${1} --> ${dir}${filename}_${count}.${extension}"
copied=1
fi
done
fi
else
cp -a "${1}" "${dir}${filename}.${extension}"
echo "${1} --> ${dir}${filename}.${extension}"
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment