dotfiles/.bin/OLD/image_daterename.sh

50 lines
1.5 KiB
Bash
Raw Normal View History

#!/bin/sh
NAME="$(basename "$1")"
EXT="$(echo ${NAME##*.} | tr '[:upper:]' '[:lower:]')"
DIR="$(dirname "$1")"
# san
SED0="$(echo "$NAME" | sed -n "/^20[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}_[0-9]\{2\}-[0-9]\{2\}-[0-9]\{2\}-[a-z]\{3\}\.$EXT/p")"
if [ ! -z "$SED0" ]
then
echo "Skipping - matches name pattern already ($NAME)"
exit 0
fi
EXIFDATES="$(exiftool -q -q \
-p '$modifyDate' \
-p '$createDate' \
-p '$dateTimeOriginal' \
-p '$FileModifyDate' \
"$1" \
| grep "^20" \
| cut -b 1-19 \
| tr ':' '-' \
| tr ' ' '_' \
)"
SED1="$(echo "$NAME" | sed -n 's/.*\(20[0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).*/\1-\2-\3_\4-\5-\6/p')"
SED2="$(echo "$NAME" | sed -n 's/.*\(20[0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)_\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).*/\1-\2-\3_\4-\5-\6/p')"
SED3="$(echo "$NAME" | sed -n 's/.*\(20[0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).*/\1-\2-\3_\4-\5-\6/p')"
PICK="$(for d in $EXIFDATES $SED1 $SED2 $SED3; do echo $d; done | sort -u | head -1)"
echo "Dates for: $1"
for e in $EXIFDATES
do echo "From EXIF: " $e; done
for f in $SED1 $SED2 $SED3
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