Skip to content

Instantly share code, notes, and snippets.

@Terrance
Last active February 14, 2026 13:32
Show Gist options
  • Select an option

  • Save Terrance/d4bdf7435d0c78f5b687daec32abd77f to your computer and use it in GitHub Desktop.

Select an option

Save Terrance/d4bdf7435d0c78f5b687daec32abd77f to your computer and use it in GitHub Desktop.
Script to detect additive-only backup files and remove redundant copies.
#!/usr/bin/env sh
trap 'rm -f "$TMPDIR"/*.addonly' INT EXIT
last=
lastf=
for curr; do
prev="$last"
last="$curr"
>&2 echo -n "$curr "
if [ "$FILTER" = "" ]; then
currf="$curr"
else
currf="$(mktemp --suffix=.addonly)"
<"$curr" $FILTER >"$currf"
echo -n "[$(wc -l "$currf")] "
fi
prevf="$lastf"
lastf="$currf"
if [ "$prev" = "" ]; then
>&2 echo "(initial)"
continue
fi
line=$("${GIT:=git}" diff --no-index --numstat "$prevf" "$currf" 2>/dev/null)
adds=$(echo $line | awk '{ print $1 }')
dels=$(echo $line | awk '{ print $2 }')
>&2 echo "(+$adds -$dels)"
if [ $dels -eq 0 ] && [ $adds -gt 0 ]; then
echo "rm '$prev'"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment