#!/bin/sh # delete-backups --- traverse directories and delete backups # Author: Noah Friedman # Created: 2000-01-30 # Public domain # $Id: delete-backups,v 1.5 2006/08/26 01:48:10 friedman Exp $ # Commentary: # GNU find 4.2 and later have a built-in --delete flag. # Use that if available. # Otherwise, if GNU find and GNU xargs are present, # use that combo with null-terminated filenames for safety. # As a final resort, run the rm command on each file. # The -n option will just print a list of the files # that would have been deleted. # Code: ponly=f case $1 in -n ) ponly=t ; shift ;; esac case $# in 0 ) echo "Usage: delete-backups {-n} [dir]" 1>&2; exit 1 ;; esac for var in find grep xargs sed rm ; do VAR=`echo "$var" | tr '[a-z]' '[A-Z]'` eval "$var=\${$VAR-$var}" done gxargp=`$xargs --version 2>&1 < /dev/null | sed -ne '/^GNU /{s/.*/t/p;q;}'` gfindp=`$find --version 2>&1 < /dev/null | sed -ne '/^GNU /{s/.*/t/p;q;}'` gfdelp=f case $gfindp in t ) if "$find" --help 2>&1 | "$grep" '.-delete' > /dev/null 2>&1 then gfdelp=t fi ;; esac set fnord "$find" ${1+"$@"} \( -name '.*~' -o -name '*~' -o -name '#*#' \) shift case $ponly:$gfdelp:$gfindp:$gxargp in t:* ) echo "[dry run]"; exec ${1+"$@"} -print ;; f:t:* ) exec ${1+"$@"} -print -delete ;; f:f:t:t ) ${1+"$@"} -print0 | "$xargs" --no-run-if-empty --null "$rm" -vf ;; f:* ) exec ${1+"$@"} -print -exec "$rm" -f '{}' \; ;; esac # delete-backups ends here