nnn/plugins/dups

47 lines
1.2 KiB
Plaintext
Raw Normal View History

#!/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
#
2020-05-06 05:29:57 +00:00
# Dependencies: find md5sum sort uniq xargs
#
# Note: bash compatible required for mktemp
#
# Shell: bash
2020-05-06 05:12:29 +00:00
# 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
2020-10-21 17:37:33 +00:00
find . -size +0 -type f -printf "%${size_digits}s %p\n" | sort -rn | uniq -w"${size_digits}" -D | sed -E '
s/^ {,12}([0-9]{,12}) (.*)$/printf "%s %s\\n" "$(md5sum "\2")" "d\1"/
' | tr '\n' '\0' | xargs -0 -n1 sh -c | 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
2020-10-21 17:37:33 +00:00
sed -e 's/md5sum.*//' "$tmpfile" | tr '\n' '\0' | xargs -0 sh -c 'rm -i "$0" "$@" < /dev/tty'
rm "$tmpfile"
2019-11-21 20:44:25 +00:00
printf "Press any key to exit"
read -r _