Summary:script that will delete old files after X numbers of days

From: Andrew Ambrose <aambrose_at_ennovatenetworks.com>
Date: Thu Sep 06 2001 - 19:06:52 EDT
Here are some of the scripts I got back...

Everybody recommended the find command::
find /yourdir/tmp -mtime +60 -exec rm {} \;

This is from Tom Payerle "Thanks Tom"
#!/bin/sh
#Script to purge scratch areas of files older than/not accessed in certain
#number of days
#
#Commands
FIND="/usr/bin/find"
RM="/usr/bin/rm"
LS=/usr"/bin/ls"
TEE="/usr/bin/tee"

#Command to use when deleting files
CMD_TO_EXEC="-exec $RM {} ;"
LSCMD="-exec $LS -l {} ;"

usage()
{
	echo "purge_scratch: Purges files on scratch areas" >&2
	echo "Usage: " >&2
	echo "purge_scratch [-d][-h][-v][-or |-and ] -f path [ -a Na ] [ -m Nm]"
>&2
	echo "	-d : Debug mode, don't delete files">&2
	echo "	-v : Verbose mode, list files to stdout">&2
	echo "	-h : Produces this help text" >&2
	echo "	-f : path to the start of the directory to prune" >&2
	echo "	-a : Specify the access time limit " >&2
	echo "	-m : Specify the modification time limit " >&2
	echo "	-and : Changes the operator between the -a and -m options to and">&2
	echo "	-or : Changes the op between the -a and -m opts to or (default)">&2
	echo "	If the file has not been accessed within Na days, or modified"
>&2
	echo "	within Nm days, it will be deleted.  If only one of Na or Nm">&2
	echo "	is given, the other is ignored.  At least one must be given.">&2
	echo "	-and and -or options ignored if only one limit given">&2
	echo "	If any option specified more than once, only the rightmost is
observed.">&2
	echo "	Only regular files are deleted.  Directories remain.">&2

}

fatal_error()
{	echo "purge_scratch: Fatal error." >&2
	echo $* >&2
	usage
	exit -1
}

# Main routine
	DEBUG=0
	VERBOSE=0
	PATH=""
	NA="-1"
	NM="-1"
	CONJ="-o"

	while [ $# -gt 0 ]
	do
	    case $1 in
		-d)
			DEBUG=1
			;;
		-v)
			VERBOSE=1
			;;
		-h)
			usage
			;;
		-f)
			if [ $# -eq 1 ]
			then
				fatal_error "Syntax error: Expecting a pathname"
			fi
			shift
			PATH="$1"
			;;
		-a)
			if [ $# -eq 1 ]
			then
				fatal_error "Syntax error: Expecting access time limit"
			fi
			shift
			NA="$1"
			;;
		-m)
			if [ $# -eq 1 ]
			then
				fatal_error "Syntax error: Expecting modification time limit"
			fi
			shift
			NM="$1"
			;;
		-or)
			CONJ="-o"
			;;
		-and)
			CONJ="-a"
			;;
		*)
			fatal_error "Syntax error: Unknown flag " $1
			;;

	    esac
	    shift
	done

	if [ "${PATH}x" = "x" ]
	then
		fatal_error "No path was given"
	fi

	if [ ! -d $PATH ]
	then
		fatal_error "path given is not a directory"
	fi

	if [ ! -r $PATH ]
	then
		fatal_error "path given is not readable"
	fi


	CMD="$CMD_TO_EXEC"
	if [ $DEBUG -ne 0 ]
	then
		#CMD="$LS -l {} ;"
		CMD=""
	fi

	TESTARG=""
	if [ $NA -gt 0 ]
	then
		TESTARG="$TESTARG -atime +$NA "
	fi

	if [ $NM -gt 0 ]
	then
		TESTARG2="-mtime +$NM "
		if [ "${TESTARG}x" != "x" ]
		then
			TESTARG="( $TESTARG  $CONJ $TESTARG2 )"
		else
			TESTARG="$TESTARG $TESTARG2"
		fi
	fi

	if [ "${TESTARG}x" = "x" ]
	then
		fatal_error "Must give either access or modification time limit"
	fi

	FULLCMD="$FIND $PATH $TESTARG -type f $LSCMD $CMD"

	TEEFILE="/dev/null"
	if [ $VERBOSE -ne 0 ]
	then
		TEEFILE=`/usr/bin/tty`
	fi
	echo $FULLCMD
	$FULLCMD | $TEE $TEEFILE
Received on Fri Sep 7 00:06:52 2001

This archive was generated by hypermail 2.1.8 : Wed Mar 23 2016 - 16:32:30 EDT