-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-photo.bash
executable file
·67 lines (65 loc) · 2.03 KB
/
rename-photo.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#
# NAME
# rename-photo.bash - Rename a photo using the datetime it was taken
#
# SYNOPSIS
# rename-photo.bash FILE
#
# DESCRIPTION
# Rename a photo to the datetime it was taken formatted as seconds since the
# Epoch
#
# EXAMPLE
# Rename more than one file using a wildcard
#
# $ rename-photo.bash *.jpg
#
safe_mv() {
echo Moving $1 to $2
if [ -e "$2" ]; then
echo WARNING: Attempted to overwrite $2
local NAME="${2%.*}"
# TODO(PM): Support numbers with more than one digit
if [ "${NAME: -2: -1}" == "-" ]; then
local NUM="${NAME: -1}" + 1
local ITERFILENAME="${NAME: 0: -1}"$NUM$EXTENSION
safe_mv $1 $ITERFILENAME$NAME$EXTENSION
else
safe_mv $1 $NAME-1.$EXTENSION
fi
else
mv $1 $2
fi
}
for FILENAME in "$@"; do
echo Processing $FILENAME
LINE=$(identify -verbose "$FILENAME" | grep "DateTime:")
echo EXIF output: $LINE
if [ -z "$LINE" ]; then
echo ERROR: Datetime not found in $FILENAME
else
IFS=$' '
read -a ARRAY <<< "${LINE:4}" # Split on ` ` and remove first 4 chars
DATE="${ARRAY[1]}"
TIME="${ARRAY[2]}"
echo Time: $TIME
if [ -z "$TIME" ]; then echo ERROR: Time not found in $FILENAME; continue; fi
IFS=$':'
read -a ARRAY <<< "${DATE}"
YEAR="${ARRAY[0]}"
echo Year: $YEAR
if [ -z "$YEAR" ]; then echo ERROR: Year not found in $FILENAME; continue; fi
MONTH="${ARRAY[1]}"
echo Month: $MONTH
if [ -z "$MONTH" ]; then echo ERROR: Month not found in $FILENAME; continue; fi
DAY="${ARRAY[2]}"
echo Day: $DAY
if [ -z "$DAY" ]; then echo ERROR: Day not found in $FILENAME; continue; fi
EPOCH="$(date --date="$MONTH/$DAY/$YEAR $TIME" "+%s")"
if [ -z "$EPOCH" ]; then echo ERROR: Invalid date found in $FILENAME; continue; fi
EXTENSION="${FILENAME##*.}"
NEWFILENAME="$EPOCH.$EXTENSION"
safe_mv $FILENAME $NEWFILENAME
fi
done