#!/bin/bash
#
# This script purges Files and Directories that are older than <days to purge>
#
# Usage: purgeFiles <directory> <days to purge>
#
# Un-Comment for Debugging
#set -xv
# Check the number of parameters
if [ $# -lt 1 ]; then
echo "Invalid Call to purgeFiles, no parameters specified"
echo "Usage: purgeFiles <directory path> [<# of days old>]"
echo " Purges files or entire directories older than the parameter"
echo " specified (default is 30 days) located in the directory path"
exit 1
fi
# Directory Path
purgePath=$1
if [ ! -d $purgePath ]; then
errWarn purgeFiles: Invalid Directory Path Specified, $purgePath
echo "Invalid Directory Path Specified, $purgePath"
exit 1
fi
# If there were 2 param's, the second param is the number of day's to purge
if [ $# -eq 2 ]; then
purgeTime=$2
else
purgeTime=30
fi
echo purgeFiles: Purging $purgePath, removing anything older than $purgeTime days old
function doPurge {
if (test -f $1) then
fileCount=$(($fileCount + 1))
echo " Purging File: $1"
rm -f $1
if [ $? -eq 0 ]; then
echo " Successfully Purged File: $1"
else
echo " Failed to Purge File: $1"
fi
elif (test -d $1) then
dirCount=$(($dirCount + 1))
echo " Purging Directory: $1"
rm -fr $1
if [ $? -eq 0 ]; then
echo " Successfully Purged Directory: $1"
else
echo " Failed to Purge Directory: $1"
fi
fi
}
fileCount=0
dirCount=0
for oldFile in `find $purgePath -mtime +$purgeTime -prune -print` ; do
doPurge $oldFile
done
echo purgeFiles: Successfully Purged $fileCount files, and $dirCount directories
exit 0