Last active
August 22, 2022 16:59
-
-
Save elisboa/631bbe8971a035c36b765f3fe19dacb4 to your computer and use it in GitHub Desktop.
A simple script to sort photo files based on their exif information
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# --- | |
#!/bin/bash -x | |
echo -e "### WELCOME TO $0\n" | |
if [[ -z ${DEST_DIR} ]] | |
then | |
TIMESTAMP=$(date +%s) | |
echo -e "Setting destination dir as ${TIMESTAMP}" | |
DEST_DIR=${TIMESTAMP} | |
else | |
echo -e "Destination dir set as ${DEST_DIR}" | |
fi | |
if [[ -n "${1}" ]] | |
then | |
echo -e "Analyzing ${1}" | |
else | |
echo -e "Argument not set. Usage: ${0} file_name.jpg" | |
echo -e "\a" | |
exit 1 | |
fi | |
for DEP in cp exiv2 mkdir rsync | |
do | |
echo -n "Checking dependencies: $DEP - " | |
if command -v $DEP > /dev/null 2>&1 | |
then | |
echo -e "OK!" | |
else | |
echo "Failed to find $DEP command. Exitting now" | |
echo -e "\a" | |
exit 1 | |
fi | |
done | |
EXIF_DATA="$(LANG=C exiv2 "${1}")" | |
if [[ -n "${EXIF_DATA}" ]] | |
then | |
echo -e "DEBUG: EXIF_DATA content is\n${EXIF_DATA}\n" | |
echo -e "Parsing information on file ${1}... " | |
FILE_EXTENSION=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep ^1 | cut -d\: -f3 | cut -d\. -f2) | |
MAKER_NAME=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera make|Image timestamp' | grep 'Camera make' | cut -d\: -f3- | cut -d\ -f2-) | |
DEVICE_NAME=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Camera model' | cut -d\: -f3- | cut -d\ -f2-) | |
FILE_YEAR=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f2- | cut -d\: -f1) | |
FILE_MONTH=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f2- | cut -d\: -f2) | |
FILE_DAY=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f2- | cut -d\: -f3 | cut -d\ -f1) | |
FILE_HOUR=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f3- | cut -d\: -f1) | |
FILE_MINUTE=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f3- | cut -d\: -f2) | |
FILE_SECOND=$(echo "$EXIF_DATA" | grep -nE 'File name|Camera model|Image timestamp' | grep 'Image timestamp' | cut -d\: -f3- | cut -d\ -f3- | cut -d\: -f3) | |
for INFORMATION in FILE_EXTENSION MAKER_NAME DEVICE_NAME FILE_YEAR FILE_MONTH FILE_DAY FILE_HOUR FILE_MINUTE FILE_SECOND | |
do | |
echo -n "Now parsing ${INFORMATION}: " | |
if [[ -z $(eval "echo \${$INFORMATION}") ]] | |
then | |
echo "FAILED. Variable \$${INFORMATION} is empty" | |
echo -e "\a" | |
exit 1 | |
else | |
echo "OK!" | |
fi | |
done | |
else | |
echo -e "No EXIF data found on file ${1}" | |
echo -e "\a" | |
exit 1 | |
fi | |
export LAST_DEST_DIR="${DEST_DIR}/${MAKER_NAME}/${DEVICE_NAME}/${FILE_YEAR}/${FILE_MONTH}/${FILE_DAY}" | |
echo -n "Creating final destination dir ${LAST_DEST_DIR}: " | |
if mkdir -p "${LAST_DEST_DIR}" | |
then | |
echo -e "OK!" | |
else | |
echo -e "FAILED. Exitting now" | |
echo -e "\a" | |
exit 1 | |
fi | |
export FILE_NAME_SHORT="${FILE_HOUR}-${FILE_MINUTE}-${FILE_SECOND}.${FILE_EXTENSION}" | |
#export FILE_NAME_FULL="${DEVICE_NAME}-${FILE_YEAR}-${FILE_MONTH}-${FILE_DAY}-${FILE_HOUR}-${FILE_MINUTE}-${FILE_SECOND}.${FILE_EXTENSION}" | |
export FILE_NAME_FULL="${DEVICE_NAME// /_}-${FILE_YEAR}-${FILE_MONTH}-${FILE_DAY}-${FILE_HOUR}-${FILE_MINUTE}-${FILE_SECOND}.${FILE_EXTENSION}" | |
echo -n "Copying file to Destination: " | |
# if cp -f -v "${1}" "${LAST_DEST_DIR}/${FILE_NAME_FULL}" > /dev/null 2>&1 | |
if rsync -ahuP "${1}" "${LAST_DEST_DIR}/${FILE_NAME_FULL}" > /dev/null 2>&1 | |
then | |
echo "${LAST_DEST_DIR}/${FILE_NAME_FULL} - full file name copy OK!" | |
else | |
echo -e "FAILED. Trying to use a shorter file name" | |
# if cp -f -v "${1}" "${LAST_DEST_DIR}/${FILE_NAME_SHORT}" > /dev/null 2>&1 | |
if rsync -ahuP "${1}" "${LAST_DEST_DIR}/${FILE_NAME_SHORT}" > /dev/null 2>&1 | |
then | |
echo "${LAST_DEST_DIR}/${FILE_NAME_SHORT} - short file name copy OK!" | |
else | |
echo -e "FAILED! Exitting now" | |
echo -e "\a" | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@debxp Quase um ano depois estou começando a realizar testes com sua maravilhosa sugestão, assim que tiver resultados sólidos atualizarei o script. Muito obrigado mesmo! <3