snippets/bash/dedupe.sh

23 lines
588 B
Bash
Raw Normal View History

2025-01-18 22:49:41 +03:00
#!/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