Operating System - HP-UX
1833197 Members
2813 Online
110051 Solutions
New Discussion

Re: Script help – record all files deleted by the script.

 
SOLVED
Go to solution
Gulam Mohiuddin
Regular Advisor

Script help – record all files deleted by the script.

Following script is being executed by Cron every night which removes some old logs files and directories.

I would like to log or record all files/directories removed by this shell script.

#!/usr/bin/sh

function purge_logs
{
find $1 -mtime +$2 -exec rm -rf {} \;
}

purge_logs /psoft/hrcat88a/LOGS 30
purge_logs /psoft/hrcat88b/LOGS 60
purge_logs /psoft/hrcat88c/LOGS 90
exit 0


Thanks,

Gulam.
Everyday Learning.
5 REPLIES 5
Jeff_Traigle
Honored Contributor
Solution

Re: Script help – record all files deleted by the script.

Something like this work for you. You can have multiple -exec clauses on find.

find $1 -mtime +$2 -exec ls -l {} \; -exec rm -rf {} \; >> logfile
--
Jeff Traigle
Patrick Wallek
Honored Contributor

Re: Script help – record all files deleted by the script.

YOu can also add a '-print' to your find command to have it print the file names.

find $1 -mtime +$2 -print -exec rm -rf {} \;
Geoff Wild
Honored Contributor

Re: Script help – record all files deleted by the script.

Use tee:
LOG=/tmp/yourlogfile


find $1 -mtime +$2 -print -exec rm -rf {} \; |tee $LOG

Rgds...Geoff



Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: Script help – record all files deleted by the script.

Hi Gulam:

Another way is this:

# find -xdev -type f -mtime +$2 | xargs -t rm -rf

The 'xargs -t' enables tracing of the command that is executed. Even more importantly, it bundles multiple file arguments to 'rm'. This eliminates a 1:1 number of execs of a separate 'rm' for every argument found by 'find'. Process creation is very, very expensive. Using 'xargs' in lieu of '-exec' with 'find' will greatly improve performance.

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Script help – record all files deleted by the script.

Hi Gulam,
you can use this way:

#!/usr/bin/sh

function purge_logs
{
find $1 -mtime +$2 -exec rm -rf {} \;
}

{
purge_logs /psoft/hrcat88a/LOGS 30
purge_logs /psoft/hrcat88b/LOGS 60
purge_logs /psoft/hrcat88c/LOGS 90
} >logfile 2>&1
exit 0

Thsi will write inti logfile all the output, including errors, comining from the 2 purge_logs.

HTH,
Art