aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/dups
blob: aef3923048c7a8d510f2343a20c231cc6fc0f842 (plain) (blame)
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
#!/usr/bin/env sh

# Description: List non-empty duplicate files in the current directory (based on size followed by MD5)
#
# Source: https://www.commandlinefu.com/commands/view/3555/find-duplicate-files-based-on-size-first-then-md5-hash
#
# Dependencies: find md5sum sort uniq xargs
#
# Note: bash compatible required for mktemp
#
# Shell: bash
# Authors: syssyphus, KlzXS

# If the size of a file has more that $size_digits digits the file will be misplaced
# 12 digits fit files up to 931GiB

EDITOR="${EDITOR:-vi}"
TMPDIR="${TMPDIR:-/tmp}"

size_digits=12
tmpfile=$(mktemp "$TMPDIR/.nnnXXXXXX")

# shellcheck disable=SC2016
find . -size +0 -type f -printf "%${size_digits}s %p\n" | sort -rn | uniq -w"${size_digits}" -D | tr '\n' '\0' | xargs -0 -n1 sh -c 'printf "%s %s\n" "$(md5sum $@)" "d$0"' | sort | { uniq -w32 --all-repeated=separate; echo; } | sed -nE '
h
s/^(.{32}).* d([0-9]*)$/md5sum: \1 size: \2 bytes/p
g

:loop
N
/.*\n$/!b loop
p' | sed -E 's/^.{32} (.*) d[0-9]*$/\1/' > "$tmpfile"

"$EDITOR" "$tmpfile"

cat "$tmpfile"

# shellcheck disable=SC2016
sed -e 's/md5sum.*//' "$tmpfile" | tr '\n' '\0' | xargs -0 sh -c 'rm -i $0 $@ < /dev/tty'

rm "$tmpfile"

printf "Press any key to exit"
read -r _