#!/bin/bash
# find-duplicates.sh - list files with identical content in a folder (and its subfolders).
# It deletes nothing: it prints groups of duplicates, you decide what to trash.
#
# Usage:  bash find-duplicates.sh ~/Downloads
#         bash find-duplicates.sh ~/Pictures > duplicates.txt
#
# How it works: a checksum (SHA-256) of every file; files with the same checksum
# have the same content, whatever their name or date. From declutr.app/blog.
set -u
dir="${1:-.}"
if [ ! -d "$dir" ]; then echo "Not a folder: $dir" >&2; exit 1; fi
echo "Checking $(find "$dir" -type f ! -name '.DS_Store' | wc -l | tr -d ' ') files in $dir..." >&2
find "$dir" -type f ! -name '.DS_Store' -size +0 -print0 \
  | xargs -0 shasum -a 256 \
  | sort \
  | awk '
      { hash = substr($0, 1, 64); path = substr($0, 67) }
      hash == prev { if (!shown) { print "\n" prevpath; groups++ } print path; extra++; shown = 1; next }
      { prev = hash; prevpath = path; shown = 0 }
      END { printf "\n%d group(s) of duplicates, %d extra cop%s.\n", groups, extra, (extra == 1 ? "y" : "ies") > "/dev/stderr" }'
