-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathphoto-date-rename
executable file
·60 lines (48 loc) · 1.15 KB
/
photo-date-rename
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
#!/bin/zsh
# Constants
readonly program="$(basename "${0}")"
# Usage
function usage {
echo "
Rename image files to the date and time they were taken.
Usage:
${program} <file...>
Options:
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
# Checks
if [[ "${1}" =~ ^(-h|--help)$ ]]
then
usage
exit 0
fi
if [[ "${#}" -lt 1 ]]
then
usage
exit 1
fi
if ! hash 'exiftool' 2> /dev/null
then
echo 'Missing "exiftool" in PATH. Install with:' >&2
echo ' brew install exiftool' >&2
exit 1
fi
# Main
for file in "${@}"
do
filename="$(basename "${file}")"
filedir="$(dirname "${file}")"
fileextension="$([[ "${file}" == *.* ]] && tr '[:upper:]' '[:lower:]' <<< ".${file##*.}" || echo '')"
pdatetime="$(exiftool -printFormat '$datetimeoriginal' "${file}" 2> /dev/null)"
if [[ -z "${pdatetime}" ]]
then
echo "$(basename "${file}"): No date found. Skipping…" >&2
continue
fi
pdate="$(cut -d' ' -f1 <<< "${pdatetime//:/-}")"
ptime="$(cut -d' ' -f2 <<< "${pdatetime//:/.}")"
newname="${pdate} ${ptime}${fileextension}"
mv "${file}" "${filedir}/${newname}"
echo "${filename}: Renamed to ${newname}"
done