Shell Script to Delete / Copy/ Zip files older than X hours

Posted By Sagar Patil

#!/bin/bash
# Delete files older than X hours.
# This script will delete files older than 3 days from $GLOBALSHARE/current/Server01
# 14 days from $GLOBALSHARE/archive/Server01 & 14 days from /backup

CurrentPath=”/globalshare/current/Server01″
ArchivePath=”/globalshare/archive/Server01″
LocalPath=”/backup”

log=/home/oracle/scripts/delete_old_files.log
err=/home/oracle/scripts/delete_old_files.err

# Use mmin variable for file modified timestamp & cmin for file creation

# Delete from Archive Directory
# 14 days = 336 hours = 336*60 = 20160
find $ArchivePath -type f -mmin +20160 -exec rm -f {} \; 1>>${log} 2>>${err}
echo “Archive File Deletion (Files Older than 14 days) finished on : ” `date` >>${log}

# Delete from Current Directory
# 36 hours = 36*60 = 2160
find $CurrentPath -type f -mmin +2160 -exec rm -f {} \; 1>>${log} 2>>${err}
echo “Current File Deletion (Files Older than 3 days) finished on : ” `date` >>${log}
exit

Leave a Reply

You must be logged in to post a comment.

Top of Page

Top menu