#!/usr/local/bin/zsh ## Time-stamp: <2001-02-14 13:47:34 ecl> ## Emilio Lopes ## Remove old backup files whose originals do not exist anymore ## or were not modified since a given amount of time. ## ## Uncomment the two lines marked with `BAK' if your system ## additionally uses the suffix `.bak' for backup files. ## ## Intended to run as a cron job. ## Require the GNU file utilities. ## THIS FILE IS IN THE PUBLIC DOMAIN. USE AT YOUR OWN RISK! emulate -R zsh setopt local_options extended_glob rc_expand_param local backup_regexp_list date_string backup_files time_stamp_file date_string='2 months ago' # must be a valid string for GNU `date' backup_regexp_list=('*~' '.*~') # globbing expressions for GNU `find' #BAK backup_regexp_list=('*.bak' $backup_regexp_list) backup_regexp_list=('-name '$backup_regexp_list) backup_files=$(find ${1:-.} -xdev -type f \( ${(j: -o :)=backup_regexp_list} \) -print0) time_stamp_file=/tmp/clean-backups_$$ touch --time=modify --date $(date --date $date_string +'%Y%m%d') $time_stamp_file local backup orig old_backups for backup in ${(pws:\C-@:)backup_files}; do orig=${backup%%(|(|.)\~[[:digit:]]##)\~} # REGEXP: (.?~[0-9]+)?~$ #BAK orig=${orig%.bak} if [[ ! -f $orig || $orig -ot $time_stamp_file ]]; then print "rm $backup [$orig]" old_backups=($old_backups $backup) fi done rm -f $time_stamp_file # $old_backups