nnn/plugins/dups

71 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env sh
2021-05-15 17:32:01 +00:00
# Description: List non-empty duplicates in the current dir (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-11-06 00:07:01 +00:00
# Dependencies: find md5sum sort uniq xargs gsed
#
2021-05-15 17:32:01 +00:00
# Notes:
# 1. If the file size exceeds $size_digits digits the file will be misplaced
# 12 digits fit files up to 931GiB
2021-05-15 20:19:52 +00:00
# 2. Bash compatible required for mktemp
#
2021-05-15 17:32:01 +00:00
# Shell: Bash
2020-05-06 05:12:29 +00:00
# Authors: syssyphus, KlzXS
EDITOR="${EDITOR:-vi}"
TMPDIR="${TMPDIR:-/tmp}"
size_digits=12
tmpfile=$(mktemp "$TMPDIR/.nnnXXXXXX")
2020-11-06 00:07:01 +00:00
printf "\
## This is an overview of all duplicate files found.
2020-11-08 12:20:36 +00:00
## Comment out the files you wish to remove. You will be given an option to cancel.
## Lines with double comments (##) are ignored.
2020-11-08 15:57:30 +00:00
## You will have the option to remove the files with force or interactively.\n
2020-11-06 00:07:01 +00:00
" > "$tmpfile"
# shellcheck disable=SC2016
2021-11-26 09:33:11 +00:00
find . -size +0 -type f -printf "%${size_digits}s %p\n" | sort -rn | uniq -w"${size_digits}" -D | sed -e '
s/^ \{0,12\}\([0-9]\{0,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
2021-11-26 09:33:11 +00:00
s/^\(.\{32\}\).* d\([0-9]*\)$/## md5sum: \1 size: \2 bytes/p
g
:loop
N
/.*\n$/!b loop
2021-11-26 09:33:11 +00:00
p' | sed -e 's/^.\{32\} \(.*\) d[0-9]*$/\1/' >> "$tmpfile"
"$EDITOR" "$tmpfile"
2020-11-08 12:20:36 +00:00
printf "Remove commented files? (yes/no) [default=n]: "
2020-11-06 00:07:01 +00:00
read -r commented
if [ "$commented" = "y" ]; then
2021-11-26 09:33:11 +00:00
sedcmd="/^##.*/d; /^[^#].*/d; /^$/d; s/^# *\(.*\)$/\1/"
2020-11-06 00:07:01 +00:00
else
printf "Press any key to exit"
read -r _
exit
fi
printf "Remove with force or interactive? (f/i) [default=i]: "
read -r force
2020-11-07 22:39:34 +00:00
if [ "$force" = "f" ]; then
#shellcheck disable=SC2016
sed -e "$sedcmd" "$tmpfile" | tr '\n' '\0' | xargs -0 -r sh -c 'rm -f -- "$0" "$@" </dev/tty'
2020-11-07 22:39:34 +00:00
else
#shellcheck disable=SC2016
sed -e "$sedcmd" "$tmpfile" | tr '\n' '\0' | xargs -0 -r sh -c 'rm -i -- "$0" "$@" </dev/tty'
2020-11-07 22:39:34 +00:00
fi
rm -- "$tmpfile"
2019-11-21 20:44:25 +00:00
printf "Press any key to exit"
read -r _