add simple deduper script

This commit is contained in:
slonkazoid 2025-01-18 22:49:41 +03:00
parent 76162edb60
commit 30c9cf4ba3
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM

22
bash/dedupe.sh Executable file
View file

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -eo pipefail
shopt -s globstar
if [[ -n "$1" ]]; then cd "$1"; fi
declare -A occurences
for file in **/*; do
if [[ ! -f "$file" || -L "$file" ]]; then continue; fi
hash=$(xxh128sum "$file" | cut -f1 -d' ')
occurence=${occurences[$hash]}
if [[ -n "$occurence" ]]; then
relpath=$(realpath -s --relative-to "$(dirname "$file")" "$occurence")
ln -sf "$relpath" "$file"
echo "symlinked ${file@Q} to ${relpath@Q}" >&2
else
echo "first occurence of $hash: ${file@Q}" >&2
occurences[$hash]=$file
fi
done