Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete file with name=month-2 1

Status
Not open for further replies.

cwsstins

MIS
Aug 10, 2004
412
US
KSH - I've got a script that is generating a number of log files like this:

y=`date +"%y"`
m=`date +"%m"`
$LOG_DIR/tuscprof_tbl_$y$m.log

I only want to keep the current 2 months' worth of files (current month and prior month). So I'm trying to come up with a way to delete any file that contains a filename month that is -2 from current month.

I thought I could do it like this, but it's not working:

Code:
if [[ -e $LOG_DIR/*_$(($y))*.log && -e $LOG_DIR/*_*$(($m - 2)).log ]]
then rm $LOG_DIR/*_*$(($m - 2)).log
fi

if [[ -e $LOG_DIR/*_$(($y - 1))%*.log && ! $m -eq 01 ]]
then rm $LOG_DIR/*_$(($y - 1))%*.log
fi

Any help appreciated...
 
why not use find with the ctime option?

find /log_dir -name "uscprof_tbl_*.log" -mtime -60 -exec rm {} \;

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Well, that deleted everything in my log directory that has been modified in the last 60 days : ) It's OK though, I'm working in dev.

I changed it to +60 and all is good. Big purple star for you!

I did a man find and I understand this statement for the most part. But can you tell me what the {} \; is doing here?
 
Hi

man find said:
-exec command ;
Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ";" is encountered. The string "{}" is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.

Feherke.
 
Opps,

Sorry about that typo, but a valuable lession learnt, backup before trying what some idiot suggests you to do ;)

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top