dotfiles/.bin/OLD/imagedate2filename.sh
2024-02-14 07:55:58 +01:00

72 lines
2.4 KiB
Bash
Executable File

#!/bin/sh
#
# usage:
# find $somedir -type f -exec imagedate2filename.sh {} \;
NAME="$(basename "$1")"
EXT="$(echo ${NAME##*.} | tr '[:upper:]' '[:lower:]')"
DIR="$(dirname "$1")"
[ "$EXT" == "jpeg" ] && EXT=jpg
# don't fiddle with file names that look right already
SED0="$(echo "$NAME" | sed -n "/^[12][09][90-4][0-9]-[0-1][0-9]-[0-3][0-9]_[0-5][0-9]-[0-5][0-9]-[0-5][0-9]-[a-z]\{3\}\.$EXT/p")"
if [ ! -z "$SED0" ]
then
echo "Skipping - matches name pattern already ($NAME)"
exit 0
fi
# find dates in exif data
EXIFDATES="$(exiftool -q -q \
-p '$modifyDate' \
-p '$createDate' \
-p '$dateTimeOriginal' \
-p '$FileModifyDate' \
"$1" \
| cut -b 1-19 \
| grep "^20" \
| tr '_' '0' \
| tr ':' '-' \
| tr ' ' '_' \
)"
# find dates in the filename
# yyyy-mm-dd_hh-mm-ss
SED1="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)-\([0-1][0-9]\)-\([0-3][0-9]\)_\([0-5][0-9]\)-\([0-5][0-9]\)-\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# yyyymmdd_hhmmss
SED2="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)\([0-1][0-9]\)\([0-3][0-9]\)_\([0-5][0-9]\)\([0-5][0-9]\)\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# yyyymmddhhmmss
SED3="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)\([0-1][0-9]\)\([0-3][0-9]\)\([0-5][0-9]\)\([0-5][0-9]\)\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# yyyy-mm-dd_hh.mm.ss
SED4="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)-\([0-1][0-9]\)-\([0-3][0-9]\)_\([0-5][0-9]\)\.\([0-5][0-9]\)\.\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# yyyy-mm-dd hh.mm.ss
SED5="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)-\([0-1][0-9]\)-\([0-3][0-9]\) \([0-5][0-9]\)\.\([0-5][0-9]\)\.\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# yyyy-mm dd_hh-mm-ss
SED6="$(echo "$NAME" | sed -n 's/.*\([12][09][90-4][0-9]\)-\([0-1][0-9]\)-\([0-3][0-9]\) \([0-5][0-9]\)-\([0-5][0-9]\)-\([0-5][0-9]\).*/\1-\2-\3_\4-\5-\6/p')"
# pick the oldest one
PICK="$(for d in $EXIFDATES $SED1 $SED2 $SED3 $SED4 $SED5 $SED6; do echo $d; done | sort -u | head -1)"
# put info on screen and rename
echo "Dates for: $1"
for e in $EXIFDATES
do echo "From EXIF: " $e; done
for f in $SED1 $SED2 $SED3 $SED4 $SED5 $SED6
do echo "From Filename: " $f; done
if [ -z "$PICK" ]
then
echo "Skipping - no dates found"
else
NEWNAME="$DIR/$PICK-$(pwgen -A -0 -1 3).$EXT"
echo mv "$1" "$NEWNAME"
mv "$1" "$NEWNAME"
fi
echo