Delete files older than X days

Version 1

#!/bin/bash
#Written Andrew Stringer
#This is to find old files and purge them to prevent backups filling
#the system up.
 
#Backup files stored to here:-
BASEDIR='/mnt/datastore/db-backup'
 
echo "Starting run at `date`" > ${BASEDIR}/housekeeping.log
 
#find files older than 2 days & delete them
rm -rf `find ${BASEDIR} -type d -mtime +1` >> ${BASEDIR}/housekeeping.log
 
exit 0

Version 2

#!/bin/bash
#find all files in IA archive directory older than ${AGE} days
#and deletes them.

#turn on debugging, -x starts, +x stops
set +x

BASE=/wworks_ftp/bar_ia
ARCHIVE=${BASE}/archive

EMAIL=${SCRIPTS}/emailtmp-$$
MAILTO=andrew.stringer@xxx.com
SENDEMAIL=no

#How old do the files need to be?
AGE=4


NUMFILES=`find ${ARCHIVE}/ -mtime +${AGE} | wc -l`

echo "Number of files found is ${NUMFILES}."


#Do the deed!
find ${ARCHIVE} -mtime +${AGE} -exec ls -l {} \;
#find ${ARCHIVE} -mtime +${AGE} -exec rm -f {} \;


exit 0

rb/deleteoldfiles.txt · Last modified: 03/10/2014 08:50 by andrew