: ########################################################################## # Title : expirefiles - expire files older than a number of days # Author : Heiner Steven # Date : 2007-06-28 # Category : File Utilities # Requires : find # SCCS-Id. : @(#) expirefiles 1.3 18/01/05 ########################################################################## # Description # ########################################################################## PN=`basename "$0"` # Program name VER='1.3' DEFAULT_CONFIG=$PN.conf FINDARGS=-L : ${root:=$HOME} usage () { echo >&2 "$PN - expire files older than a number of days usage: $PN [-hn] [-c config] -c: use the specified configuration file (default: $DEFAULT_CONFIG) -n: only print what would be done, do not remove anything -h: print this usage summary The configuration file is searched in the current directory and in $HOME/etc ." exit 1 } msg () { echo >&2 "$PN:" "$@"; } fatal () { msg "$@"; exit 1; } ########################################################################## # initialconfig - write initial configuration file ########################################################################## initialconfig () { cat < "$DEFAULT_CONFIG" || fatal "ERROR: could not write configuration file: $DEFAULT_CONFIG" fatal "INFO: wrote example configuration file: $DEFAULT_CONFIG" fi [ -r "$config" ] || fatal "ERROR: cannot read configuration file: $config" [ -s "$config" ] || fatal "ERROR: cannot read configuration file: $config" if [ "$DryRun" = true ] then findaction="-print" else # Remove matching files. If your local find(1) does not support the # POSIX internal xargs operator ("{} +") replace the '+' with ";". findaction='-print -exec rm -f -- {} +' fi # Remove comment lines and empty lines. Collapse multiple TAB (ASCII 9) # characters to one TAB. sed -e '/^[ ]*#/d' \ -e '/^[ ]*$/d' \ -e 's/ */ /g' "$config" | { OIFS=$IFS; newIFS=" " IFS=$newIFS while read dir pattern time do IFS=$OIFS if [ -z "$dir" ] || [ -z "$pattern" ] || [ -z "$time" ] then msg "ERROR: invalid line: $config" msg "lines need to have the format" \ "DIRECTORYPATTERHDAYS:" fatal "$dir $pattern $time" fi #echo >&2 "DEBUG: dir=<$dir> pattern=<$pattern> time=<$time>" # Relative paths (not starting with '/') are always relative to the # HOME directory case "$dir" in /*) absdir=$dir;; *) absdir=$root/$dir;; esac [ -d "$absdir" ] || fatal "ERROR: directory does not exist: $dir" case "$time" in [+-]*) ;; *) msg "WARNING: number of days should start with '+'/'-'";; esac # Run the find(1) command to delete files. Standard input of # find is redirected because it should not run interactively echo "### expiring $dir ###" find $FINDARGS "$absdir" -name "$pattern" -mtime "$time" \ $findaction < /dev/null IFS=$newIFS done } exit 0