-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageRename
executable file
·242 lines (207 loc) · 5.22 KB
/
imageRename
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
# Rename image files using their exif or last modification timestamp
# necessary packages: libimage-exiftool-perl
# define global default values
DRYRUN='false'
EXIFUPDATE='false'
IMAGE_PATH="."
PATTERN="%Y-%m-%d---%H-%M-%S"
#######################################
# Generates new filename
# Globals:
# None
# Arguments:
# $1 timestamp of file as string
# $2 previous filename
# Returns:
# new filename
#######################################
generate_filename() {
local datetime
local old_filename
readonly datetime="${1}"
readonly old_filename="${2}"
local extension
readonly extension="$(echo "${old_filename}" | grep -o '[^.]*$')"
local counter=0
local new_filename="${datetime}.${extension}"
while [[ "${new_filename}" != "${old_filename}" && -f "${new_filename}" ]]; do
counter=$((counter + 1))
new_filename="${datetime} (${counter}).${extension}"
done
echo "${new_filename}"
return 0
}
#######################################
# Get exif timestamp from file
# Globals:
# PATTERN
# Arguments:
# $1 filename
# Returns:
# timestamp
#######################################
get_exif_timestamp() {
local filename
readonly filename="${1}"
# get datetime from exif data
local exif_create_date
exif_create_date="$(exiftool -t -s -d "${PATTERN}" -CreateDate "${filename}")"
local timestamp="${exif_create_date#CreateDate[[:space:]]*}"
echo "${timestamp}"
return 0
}
#######################################
# Set exif timestamp to last modified
# timestamp of file
# Globals:
# EXIFUPDATE
# DRYRUN
# Arguments:
# $1 filename
# Returns:
# None
#######################################
set_exif_timestamp_to_last_modified() {
local filename
readonly filename="${1}"
if [[ "${EXIFUPDATE}" == 'true' ]]; then
local exif_timestamp
exif_timestamp="$(date -r "${filename}" "+%Y-%m-%d %H:%M:%S")"
if [[ "${DRYRUN}" == 'false' ]]; then
exiftool -overwrite_original_in_place \
-CreateDate="${exif_timestamp}" "${filename}" 2>&1 >/dev/null
fi
fi
return 0
}
########################################
# Get exif timestamp from file. If not
# available, get last modified timestamp
# Globals:
# PATTERN
# EXIFUPDATE
# DRYRUN
# Arguments:
# $1 filename
# Returns:
# 0: exif timestamp
# 1: last modified timestamp
########################################
get_timestamp() {
local filename
readonly filename="${1}"
# get datetime from exif data
local timestamp
timestamp=$(get_exif_timestamp "${filename}")
# if not available, get last modification time from file
if [[ -z "${timestamp}" ]]; then
timestamp="$(date -r "${filename}" "+${PATTERN}")"
$(set_exif_timestamp_to_last_modified "${filename}")
echo "${timestamp}"
return 1 # indicate that timestamp if not exif, but last modified
fi
echo "${timestamp}"
return 0
}
########################################
# Rename a specified file
# Globals:
# DRYRUN
# Arguments:
# $1 old filename
# $2 new filename
# Returns:
# message
########################################
rename_file() {
local old_filename
local new_filename
readonly old_filename="${1}"
readonly new_filename="${2}"
if [[ "${old_filename}" != "${new_filename}" ]]; then
echo "${old_filename} --> ${new_filename}"
if [[ "${DRYRUN}" == 'false' ]]; then
mv -- "${old_filename}" "${new_filename}"
fi
else
echo "keep ${new_filename}"
fi
return 0
}
########################################
# Bind all parameters to flags and path
# Globals:
# DRYRUN
# EXIFUPDATE
# IMAGE_PATH
# PATTERN
# Arguments:
# $@ all arguments of script
# Returns:
# None
########################################
bind_arguments() {
local flag
while getopts 'dep:' flag; do
case "${flag}" in
d) DRYRUN='true' ;;
e) EXIFUPDATE='true' ;;
p) PATTERN="${OPTARG}" ;;
esac
done
shift $((OPTIND - 1)) # image path is now in $1
readonly DRYRUN
readonly EXIFUPDATE
readonly PATTERN
# validate arguments
if [[ "$#" -ne 1 ]]; then
echo "Expecting exactly one parameter. Exiting"
exit 1
fi
readonly IMAGE_PATH="${1}"
return 0
}
########################################
# Write information about DRYRUN mode
# Globals:
# DRYRUN
# Arguments:
# None
# Returns:
# None
########################################
echo_dryrun_info() {
if [[ "${DRYRUN}" == 'true' ]]; then
echo ""
echo "NOTE: This is a DRYRUN only"
echo "---------------------------"
echo ""
fi
return 0
}
main() {
# read script arguments
bind_arguments "$@"
if [[ ! -d "${IMAGE_PATH}" ]]; then
echo "Path '${IMAGE_PATH}' does not exist. Existing"
exit 2
fi
echo_dryrun_info
cd "${IMAGE_PATH}"
# loop through all files, use \n as separator to support whitespaces in
# filename; order alphabetically while interpret numbers
local filename
local timestamp
local new_filename
find . -type f -printf "%f\n" | sort -V | while read filename; do
timestamp=$(get_timestamp "${filename}")
if [[ $? -eq 1 ]]; then
echo "${filename}: last modification time used"
fi
new_filename=$(generate_filename "${timestamp}" "${filename}")
rename_file "${filename}" "${new_filename}"
done
}
# call main function and hand over all parameters
main "$@"