Anyone have or seen a script that scans a directory and moves files/folders that are older than X number of days? I would like to declutter my downloads folder and only keep the last week worth of downloads in the top folder.
#!/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
Anyone have or seen a script that scans a directory and moves files/folders that are older than X number of days? I would like to declutter my downloads folder and only keep the last week worth of downloads in the top folder.
You could certainly set a smart folder to find those files based on date, but it doesn't look like you can then apply an action (or run a script) periodically based on what is found in the search (at least in Tiger, not sure about Leopard). That's a good idea for smart folder functionality though, I'd like to see it. You could just set up rules like in Mail and how often or under what conditions to apply the rules.I think you can set up a smart folder to do that.