Shell script to delete files and folders older than x days
This script finds, logs and deletes files and folders older than x days.
#/bin/bash
DIRS="/dir1/,/path/to/dir2/,/dir3/"
LOG_FILE="path to log file"
TIMESTAMP=date +%Y:%m:%d
DAYS_OLD="10" # This will find and delete folders and files older than 10 days.
IFS=$','
echo "....................................." >> $LOG_FILE
echo "Starting Deletion job on : $TIMESTAMP" >> $LOG_FILE
for DIR in $DIRS
do
echo "Looking for Files in $DIR" >> $LOG_FILE
find $DIR -type f -mtime $DAYS_OLD >> $LOG_FILE
find $DIR -type f -mtime $DAYS_OLD -exec rm -f {} ;
echo "Deleted found files" >> $LOG_FILE
echo "Now Looking for Empty Folders Older than $DAYS_OLD Days"
find $DIR -type d -empty -mtime +$DAYS_OLD >> $LOG_FILE
find $DIR -type d -empty -mtime +$DAYS_OLD -exec rm -rf {} ;
done
unset IFS
echo "Cleanup on $NOW completed" >> $LOG_FILE
echo "..........................................................">>$LOG_FILE