Thursday, November 19, 2015

Shell Script for log file cleanup

Problem :

lets find the list of file that are older than 30days. That has extension of *.log

-rw-r--r-- 1 oracle oinstall        513 Jul 28 08:27 change_pass.sql
-rw-r----- 1 oracle oinstall     188416 Jul 30 11:37 testnetlink.dmp
-rw-r--r-- 1 oracle oinstall       1477 Jul 30 11:37 impi_temp.log
-rw-r--r-- 1 oracle oinstall       1433 Jul 30 12:58 impi1_temp.log

Requirement :

Now I want to remove the files that are older than 30 days and at same time I want keep a log of the files that are being deleted.


### Script starts here ###

#!/usr/bin/ksh

cd /u01/app/oracle/dpump; ## location where I want to cleanup the log files
echo " we are now in : `pwd`"


for FILE in `find /u01/app/oracle/dpump/*.log -mtime +30`
   do echo $FILE
   rm -f $FILE ##2>/dev/null # Gets rid of directory or permission errors
done  2>&1 1> /u01/app/oracle/dpump/log.txt




sample output of log file :

lets see what is captured in cleanup log file.

cat u01/app/oracle/dpump/log.txt

u01/app/oracle/dpump/AUDIT_PART1.log
/u01/app/oracle/dpump/impi1_temp.log
/u01/app/oracle/dpump/impi_temp.log
/u01/app/oracle/dpump/test1.log

No comments:

Post a Comment