Skip to content

Instantly share code, notes, and snippets.

@Kamillaova
Last active June 16, 2021 16:19
Show Gist options
  • Save Kamillaova/a9715fd4de78427a162c586386f5bdf2 to your computer and use it in GitHub Desktop.
Save Kamillaova/a9715fd4de78427a162c586386f5bdf2 to your computer and use it in GitHub Desktop.
Sort images by date (copy to year/month directories)
#!/usr/bin/env bash
LC_ALL=C
if ! [ -x "$(command -v identify)" ]; then
echo 'Error: imagemagick is not installed.' >&2
exit 1
fi
IMGS=./imgs
OUT=./sort
DATELIST=$(identify -format "%f!%[EXIF:DateTime]\n" ${IMGS}/* | sed 's/ ..:..:..//' | cut -d':' -f-2)
IFS=$'\n'
for img in ${DATELIST}; do
DATE=$(echo "${img}" | rev | cut -d! -f1 | rev)
FILE=$(echo "${img}" | cut -d! -f1)
YEAR=$(echo "${DATE}" | cut -d: -f1)
MONTH=$(echo "${DATE}" | cut -d: -f2)
if ! [ -d "${OUT}/${YEAR}" ]; then
echo -e "Creating \e[36mYear\e[39m ${YEAR} (${OUT}/${YEAR})"
mkdir -p "${OUT}/${YEAR}"
fi
if ! [ -d "${OUT}/${YEAR}/${MONTH}" ]; then
echo -e "Creating \e[93mMonth\e[39m ${MONTH} (${OUT}/${YEAR}/${MONTH})"
mkdir -p "${OUT}/${YEAR}/${MONTH}"
fi
echo -e "\e[92mCopying \e[34m${FILE}\e[92m to \e[34m${OUT}/${YEAR}/${MONTH}/${FILE}\e[39m"
cp "${IMGS}/${FILE}" "${OUT}/${YEAR}/${MONTH}/${FILE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment