forked from linux4ever07/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm_md5sum.sh
executable file
·60 lines (46 loc) · 1.17 KB
/
rm_md5sum.sh
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/bash
# This script removes duplicate files in the directory given as
# argument. The files with the oldest modification date will be
# considered to be the originals, when other files with the same MD5
# hash are found.
set -eo pipefail
usage () {
printf '\n%s\n\n' "Usage: $(basename "$0") [dir]"
exit
}
if [[ -z $1 || ! -d $1 ]]; then
usage
fi
dn=$(readlink -f "$1")
mapfile -t files < <(find "$dn" -type f 2>&-)
declare -A md5s_date md5s_fn md5s_og
for (( i = 0; i < ${#files[@]}; i++ )); do
fn="${files[${i}]}"
md5_fn=$(md5sum -b <<<"$fn")
md5_fn="${md5_fn%% *}"
md5=$(md5sum -b "$fn")
md5="${md5%% *}"
date=$(stat -c '%Y' "$fn")
if [[ -n ${md5s_date[${md5}]} ]]; then
if [[ $date -lt ${md5s_date[${md5}]} ]]; then
md5s_date["${md5}"]="$date"
md5s_og["${md5}"]="$fn"
fi
else
md5s_date["${md5}"]="$date"
md5s_og["${md5}"]="$fn"
fi
md5s_fn["${md5_fn}"]="$md5"
done
for (( i = 0; i < ${#files[@]}; i++ )); do
fn="${files[${i}]}"
md5_fn=$(md5sum -b <<<"$fn")
md5_fn="${md5_fn%% *}"
md5="${md5s_fn[${md5_fn}]}"
if [[ -n ${md5s_og[${md5}]} ]]; then
if [[ $fn != "${md5s_og[${md5}]}" ]]; then
printf '%s\n' "$fn"
rm -f "$fn"
fi
fi
done