####################################################################### # age_rm - remove files in X directory that meet Y criteria # allowing for recursion # J. David Schronce - Mon Jul 16 16:05:33 CDT 2007 ####################################################################### # JDS 071127 - fixed * REGEX issue ####################################################################### LOGDIR=$YITWRK/logs/age_rm LOGFILE=age_rm.`date '+%y%m%d-%H%M%S`.log LOGFILE=$LOGDIR/$LOGFILE RECOVERDIR=$LOGDIR/recover RECOVERDAYS=30 USAGE="\nUSAGE: $0 [-r|-R] DirectoryName REGEX/ALL Age(in days) \n-r (or -R) for RECURSION is optional \nNote: \"*\" is an unsupported REGEX - use ALL to remove all files.\n" ####################################################################### # Check for parms ####################################################################### if [ $# -lt 3 ] || [ $# -gt 4 ]; then # echo "\nUSAGE: $0 [-r|-R] DirectoryName REGEX Age(in days)" # echo "-r (or -R) for RECURSION is optional\n" echo $USAGE exit fi if [ $# -eq 4 ]; then RECURSE=$1 if [ $RECURSE != "-r" ] && [ $RECURSE != "-R" ]; then # echo "\nUSAGE: $0 [-r|-R] DirectoryName REGEX/ALL Age(in days)" # echo "-r (or -R) for RECURSION is optional" # echo "Note: * is an unsupported REGEX - use ALL to remove all files.\n" echo $USAGE exit else PRUNE="" RECURSE="full recursion" fi TARGETDIR=$2 echo $3 case $3 in ALL|All|ALl|all|aLL|alL) PARM="";; *) PARM="-name $3";; esac AGE=$4 fi if [ $# -eq 3 ]; then TARGETDIR=$1 echo $2 case $2 in ALL|All|ALl|all|aLL|aLl|AlL|alL) PARM="";; *) PARM="-name $2";; esac # PARM="-name $2" AGE=$3 PRUNE="-depth 0" PRUNE="-prune" RECURSE="no recursion" fi ####################################################################### # Validate TARGETDIR ####################################################################### if [ $TARGETDIR = "/" ]; then echo "\n\"/\" is an invalid starting directory..\n" exit fi if [ -d $TARGETDIR ]; then cd $TARGETDIR 2> /dev/null else echo "\n$TARGETDIR does not exist..\n" exit fi ####################################################################### # Count number of matching files (use 'bc' to strip leading spaces) ####################################################################### #FOUND=`find . -name "$PARM" -type f -mtime +$AGE $PRUNE 2> /dev/null | wc -l` echo "find . "$PARM" -type f -mtime +$AGE $PRUNE" find . "$PARM" -type f -mtime +$AGE $PRUNE FOUND=`find . "$PARM" -type f -mtime +$AGE $PRUNE 2> /dev/null | wc -l` FOUND=`echo "$FOUND + 0" | bc` ####################################################################### # Quit if nothing to do ####################################################################### if [ $FOUND -eq 0 ]; then echo "\n$FOUND Files to remove - exiting...\n" exit fi ####################################################################### # Initialize LOGFILE ####################################################################### echo "Files Removed: $FOUND" > $LOGFILE echo "Matching $PARM, at least $AGE days old." >> $LOGFILE echo "Starting in: $TARGETDIR with $RECURSE.\n" >> $LOGFILE ####################################################################### # Process files ####################################################################### if [ ! -d $RECOVERDIR ]; then echo "\nRECOVERY DIRECTORY $RECOVERDIR does not exist...\nExiting..." echo "Create $RECOVERDIR and try again...\n" exit fi for foundfile in `find . -name "$PARM" -type f -mtime +$AGE $PRUNE`; do ls -l $foundfile >> $LOGFILE cd `dirname $foundfile` 2> /dev/null echo `basename $foundfile` >> $LOGFILE 2>> $LOGFILE cp `basename $foundfile` $RECOVERDIR rm `basename $foundfile` cd $TARGETDIR 2> /dev/null done ####################################################################### # Remove files from RECOVERDIR over RECOVERDAYS old ####################################################################### find $RECOVERDIR -mtime +$RECOVERDAYS -exec rm {} \;