-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-dupes.sh.in
executable file
·468 lines (391 loc) · 10.3 KB
/
clean-dupes.sh.in
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#!/usr/bin/env bash
usage() {
local old_xtrace
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
{
echo "${script_name} - Move duplicate files to a backup directory."
echo "Usage: ${script_name} [flags] src-directory [src-directory]..."
echo "Option flags:"
echo " -b --backup-dir - Backup directory. Default: '${backup_dir}'."
echo " -d --dupes-list - Dupes list file. Default: '${dupes_list}'."
echo " -m --moves-list - Moves list file. Default: '${moves_list}'."
echo " -k --keep-pos - Moves list keep position {all, 1, 2, last}. Default: '${keep_pos}'."
echo " -h --help - Show this help and exit."
echo " -v --verbose - Verbose execution."
echo " -g --debug - Extra verbose execution."
echo "Option steps:"
echo " -1 --gen-dupes - Find duplicate files and generate a dupes list. Does not move files. Default: '${gen_dupes}'."
echo " -2 --gen-moves - Generate a moves list from a dupes list. Does not move files. Default: '${gen_moves}'."
echo " -3 --move-files - Move files in moves list to the backup directory. Default: '${move_files}'."
echo "Info:"
print_project_info
} >&2
eval "${old_xtrace}"
}
process_opts() {
local short_opts="b:d:m:k:hvg123"
local long_opts="backup-dir:,dupes-list:,moves-list:,keep-pos:,help,\
verbose,debug,gen-dupes,gen-moves,move-files"
local opts
opts=$(getopt --options ${short_opts} --long ${long_opts} -n "${script_name}" -- "$@")
eval set -- "${opts}"
while true ; do
# echo "${FUNCNAME[0]}: (${#}) '${*}'"
case "${1}" in
-b | --backup-dir)
backup_dir="${2}"
shift 2
;;
-d | --dupes-list)
dupes_list="${2}"
shift 2
;;
-m | --moves-list)
moves_list="${2}"
shift 2
;;
-k | --keep-pos)
keep_pos="${2}"
shift 2
;;
-h | --help)
usage=1
shift
;;
-v | --verbose)
verbose=1
shift
;;
-g | --debug)
verbose=1
debug=1
set -x
shift
;;
-1 | --gen-dupes)
gen_dupes=1
shift
;;
-2 | --gen-moves)
gen_moves=1
shift
;;
-3 | --move-files)
move_files=1
shift
;;
--)
shift
src_dirs=("$@")
break
;;
*)
echo "${script_name}: ERROR: Internal opts: '${*}'" >&2
exit 1
;;
esac
done
}
on_exit() {
local result=${1}
local sec=${SECONDS}
set +x
echo "${script_name}: Done: ${result}, ${sec} sec ($(sec_to_min "${sec}") min)." >&2
}
on_err() {
local f_name=${1}
local line_no=${2}
local err_no=${3}
echo "${script_name}: ERROR: function=${f_name}, line=${line_no}, result=${err_no}" >&2
exit "${err_no}"
}
print_project_banner() {
echo "${script_name} (@PACKAGE_NAME@) - ${start_time}"
}
print_project_info() {
echo " @PACKAGE_NAME@ ${script_name}"
echo " Version: @PACKAGE_VERSION@"
echo " Project Home: @PACKAGE_URL@"
}
sec_to_min() {
local sec=${1}
local min=$(( sec / 60 ))
local frac_10=$(( (sec - min * 60) * 10 / 60 ))
local frac_100=$(( (sec - min * 60) * 100 / 60 ))
if (( frac_10 != 0 )); then
frac_10=''
fi
echo "${min}.${frac_10}${frac_100}"
}
check_file() {
local msg="${1}"
local file="${2}"
if [[ ! -f "${file}" ]]; then
echo "${script_name}: ERROR: ${msg} not found: '${file}'" >&2
exit 1
fi
}
check_program() {
local prog="${1}"
local path="${2}"
if ! test -x "$(command -v "${path}")"; then
echo "${script_name}: ERROR: Please install '${prog}'." >&2
exit 1
fi
}
check_find_dupes() {
if ! test -x "$(command -v "${find_dupes}")"; then
echo "${script_name}: ERROR: Please install find-dupes program." >&2
exit 1
fi
}
check_pos() {
local kp=${1}
case "${kp}" in
all | 1 | 2 | last)
;;
*)
echo "${script_name}: ERROR: Bad --keep-pos '${kp}'." >&2
exit 1
;;
esac
}
check_src_dirs() {
local sd=("${@}")
#echo "${FUNCNAME[0]}: src dirs: @${@}@" >&2
#echo "${FUNCNAME[0]}: count: @${#sd[@]}@" >&2
if [[ ${#sd[@]} -eq 0 ]]; then
echo "${script_name}: ERROR: No source directories given." >&2
usage
exit 1
fi
for ((i = 0; i < ${#sd[@]}; i++)); do
if [[ -d "${sd[i]}" ]]; then
[[ ${verbose} ]] && echo "${FUNCNAME[0]}: [$((i + 1))] '${sd[i]}' OK." >&2
else
echo "${script_name}: ERROR: Bad source directory: [$((i + 1))] '${sd[i]}'." >&2
usage
exit 1
fi
done
[[ ${verbose} ]] && echo "" >&2
return 0
}
check_backup() {
local bd=${1}
if [[ -e ${bd} ]]; then
echo "${script_name}: ERROR: Backup directory exists: '${bd}'." >&2
exit 1
fi
}
process_group() {
local group=("${@}")
local count="${#group[@]}"
local debug=''
if [[ ${count} -eq 0 ]]; then
echo "${FUNCNAME[0]}: ERROR: Empty group." >&2
exit 1
fi
local i
if [[ "${keep_pos}" == 'all' ]]; then
for ((i = 0; i < count; i++)); do
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}: Keep (all) [$((i + 1))] '${group[i]}'" >&2
fi
echo "# [$((i + 1))] ${group[i]}" >> "${moves_list}"
done
elif [[ "${keep_pos}" == 'last' ]]; then
for ((i = 0; i < count - 1; i++)); do
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}: Move (last) [$((i + 1))] '${group[i]}'" >&2
fi
echo "[$((i + 1))] ${group[i]}" >> "${moves_list}"
done
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}: Keep (last) [$((i + 1))] '${group[i]}'" >&2
fi
echo "# [$((i + 1))] ${group[i]}" >> "${moves_list}"
else
for ((i = 0; i < count; i++)); do
if [[ $((i + 1)) -ne ${keep_pos} ]]; then
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}: Move (${keep_pos}) [$((i + 1))] '${group[i]}'" >&2
fi
echo "[$((i + 1))] ${group[i]}" >> "${moves_list}"
else
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}: Keep (${keep_pos}) [$((i + 1))] '${group[i]}'" >&2
fi
echo "# [$((i + 1))] ${group[i]}" >> "${moves_list}"
fi
done
fi
echo "" >> "${moves_list}"
}
generate_moves() {
local -a group=()
local line_in
local line_no=0
local debug=''
echo "${script_name}: INFO: Generating moves list..." >&2
echo "# Generated moves, keep-pos = '${keep_pos}'" > "${moves_list}"
echo "# $(date '+%a %d %b %Y %r %Z')" >> "${moves_list}"
while read -r line_in; do
((line_no += 1))
if [[ "${line_in:0:1}" == "#" ]]; then
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: Skip '${line_in}'" >&2
fi
echo "${line_in}" >> "${moves_list}"
continue
fi
if [[ ! ${line_in} ]]; then
if [[ ${#group[@]} -eq 0 ]]; then
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: Empty group." >&2
fi
echo '' >> "${moves_list}"
else
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: Have group (${#group[@]})." >&2
fi
process_group "${group[@]}"
group=()
fi
continue
fi
local regex="^\[([[:digit:]]+)\] ([[:print:]]+)$"
if [[ ! "${line_in}" =~ ${regex} ]]; then
echo "${FUNCNAME[0]}:${line_no}: ERROR: Match failed: '${line_in}'" >&2
exit 1
fi
local pos
local file
pos="${BASH_REMATCH[1]}"
file="${BASH_REMATCH[2]}"
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: [${pos}] = '${file}'" >&2
fi
group+=("${file}")
done < "${dupes_list}"
if [[ ${line_in} ]]; then
process_group "${group[@]}"
fi
}
move_files() {
local line_in
local line_no=0
local debug=''
while read -r line_in; do
(( line_no = line_no + 1 ))
if [[ "${line_in:0:1}" == "#" || ! ${line_in} ]]; then
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: Skip '${line_in}'" >&2
fi
continue
fi
local regex="^\[([[:digit:]]+)\] ([[:print:]]+)$"
if [[ ! "${line_in}" =~ ${regex} ]]; then
echo "${FUNCNAME[0]}:${line_no}: ERROR: Match failed: '${line_in}'" >&2
exit 1
fi
local pos
local file
pos="${BASH_REMATCH[1]}"
file="${BASH_REMATCH[2]}"
file="$(realpath "${file}")"
if [[ ${debug} ]]; then
echo "${FUNCNAME[0]}:${line_no}: Move [${pos}] = '${file}'" >&2
fi
if [[ ! -e ${file} ]]; then
echo "${FUNCNAME[0]}:${line_no}: ERROR: No Such file: '${line_in}'" >&2
exit 1
fi
local dest_dir
dest_dir="${backup_dir}${file%/*}"
mkdir -p "${dest_dir}"
mv --verbose "${file}" "${dest_dir}/"
done < "${moves_list}"
}
#===============================================================================
export PS4='\[\e[0;33m\]+ ${BASH_SOURCE##*/}:${LINENO}:(${FUNCNAME[0]:-main}):\[\e[0m\] '
script_name="${0##*/}"
# SCRIPTS_TOP=${SCRIPTS_TOP:-"$(cd "${BASH_SOURCE%/*}" && pwd)"}
SECONDS=0
start_time="$(date +%Y.%m.%d-%H.%M.%S)"
trap "on_exit 'Failed'" EXIT
trap 'on_err ${FUNCNAME[0]:-main} ${LINENO} ${?}' ERR
trap 'on_err SIGUSR1 ? 3' SIGUSR1
set -eE
set -o pipefail
set -o nounset
find_dupes="${find_dupes:-find-dupes}"
backup_dir=''
dupes_list=''
moves_list=''
keep_pos=''
usage=''
verbose=''
gen_dupes=''
gen_moves=''
move_files=''
declare -a src_dirs=()
process_opts "${@}"
backup_dir="${backup_dir:-/tmp/${script_name%.sh}-${start_time}}"
backup_dir="$(realpath "${backup_dir}")"
dupes_list="${dupes_list:-${backup_dir}/dupes.lst}"
dupes_list="$(realpath --canonicalize-missing "${dupes_list}")"
keep_pos="${keep_pos:-all}"
moves_list="${moves_list:-${dupes_list%/*}/moves-keep-${keep_pos}.lst}"
moves_list="$(realpath --canonicalize-missing "${moves_list}")"
if [[ ! ${gen_dupes} && ! ${gen_moves} && ! ${move_files} ]]; then
gen_dupes=1
gen_moves=1
fi
if [[ ${usage} ]]; then
usage
trap - EXIT
exit 0
fi
if [[ ${gen_dupes} ]]; then
check_program "find-dupes" "${find_dupes}"
check_backup "${backup_dir}"
check_src_dirs "${src_dirs[@]}"
mkdir -p "${backup_dir}"
"${find_dupes}" ${verbose:+--verbose} --file-list --output-dir="${backup_dir}" "${src_dirs[@]}"
if [[ "${backup_dir}/dupes.lst" != "${dupes_list}" ]]; then
mv -f "${backup_dir}/dupes.lst" "${dupes_list}"
fi
if [[ ${verbose} ]]; then
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
echo '---| dupes.lst |----------------' >&2
cat "${dupes_list}" >&2
echo '--------------------------------' >&2
eval "${old_xtrace}"
fi
fi
if [[ ${gen_moves} ]]; then
check_pos "${keep_pos}"
check_file '--dupes-list' "${dupes_list}"
generate_moves
if [[ ${verbose} ]]; then
old_xtrace="$(shopt -po xtrace || :)"
set +o xtrace
echo '---| moves.lst |----------------' >&2
cat "${moves_list}" >&2
echo '--------------------------------' >&2
eval "${old_xtrace}"
fi
fi
if [[ ${move_files} ]]; then
check_file '--moves-list' "${moves_list}"
move_files
echo "${script_name}: Duplicate files moved to '${backup_dir}'."
trap $'on_exit "Success"' EXIT
exit 0
fi
echo "${script_name}: File lists generated in '${moves_list%/*}'."
trap $'on_exit "Success"' EXIT
exit 0