Skip to content

Instantly share code, notes, and snippets.

@frafra
Last active March 14, 2022 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frafra/59a276250c57cedac454d43dafe80347 to your computer and use it in GitHub Desktop.
Save frafra/59a276250c57cedac454d43dafe80347 to your computer and use it in GitHub Desktop.
Find identical files and folders (comparing file names, paths and sizes)
#!/bin/bash
#
# Example:
# $ ./find-dupes-dir.sh $HOME/Documents/*/
set -Eeuo pipefail
declare -A dirs
for dir in "$@"
do
cd "$dir"
checksum="$(find . -printf '%p %b\n' | sort | shasum - | cut -f1 -d' ')"
if [[ -z "${dirs[$checksum]-}" ]]
then
dirs[$checksum]="$dir"
else
>&2 echo "$dir is a duplicate of ${dirs[$checksum]}"
echo "$dir"
fi
cd "$OLDPWD"
done
#!/bin/bash
#
# Example:
# $ fd -e jpg . $HOME/Pictures | ./find-dupes.sh
set -Eeuo pipefail
declare -A paths
while read path
do
filename="${path##*/}"
identifier="$filename/$(stat --printf='%s' "$path")"
if [[ -z "${paths[$identifier]-}" ]]
then
paths[$identifier]="$path"
else
>&2 echo "$path is a duplicate of ${paths[$identifier]}"
echo "$path"
fi
done
@frafra
Copy link
Author

frafra commented Mar 14, 2022

The output (stdout) can be stored to a file or just piped into xargs -d$'\n' rm for immediate removal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment