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!

Expert eyes required 2

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Guys, I have written a script that should (!) copy a log, append a date and remove any existing files over 7 days.
This is fairly straightforward, however, as this is going to be put into a live / production system I would very much like the experts to cast an eye over it and let me know if this is OK.

#!/bin/ksh
#
# Copy Meridian Log and delete logs older than 7 days.
#
TODAY=`date +%d/%m/%y | tr -s '/' '-'`
LOG_DIR=/opt/admin/scripts/logcheck
LOG=/opt/admin/scripts/logcheck/file1

[[ -f $LOG ]] && /usr/bin/cp -p $LOG $LOG.$TODAY
/usr/bin/find $LOG_DIR -mtime +7 -exec rm {} \; >/dev/null


Any help / ideas would be gratefully received.
Thanks
Alan
 
Looks ok to me

but why not test

-e exists & -s not empty

to save copying empty files

[ -e $LOG -a -s $LOG ] && /usr/bin/cp -p $LOG $LOG.$TODAY

also

TODAY=`date +%d-%m-%y`

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Mike,

Thanks for the tips, very helpful.
I had no idea I could write the date with that format.

Cheers
Alan
 
Hi,

if you want your final date to be DD-MM-YY, you can obtain it directly without transformation.
Code:
TODAY=$(date +%d-%m-%y)

Before deleting files over 7 days, mybe you need to list them in a 7daysold.log before deleting, this way, you kow exactly what you have deleted if you are asked for.
Code:
/usr/bin/find $LOG_DIR -mtime +7 -exec ls -l {}\; >> /tmp/7daysold.log
/usr/bin/find $LOG_DIR -mtime +7 -exec rm {} \;

Ali
 
Thanks Ali, I am learning a lot from this forum.
Haven't forgotten that you helped me out last week as well.

Cheers
Alan
 
Alan - if you hadn't forgotten, perhaps Ali deserves a star for his efforts?

Alan Bennett said:
I don't mind people who aren't what they seem. I just wish they'd make their mind up.
 
OK Ken, that is a fair point so consider it done. But I can't leave out Mike, so he can have one too.
Sorry for neglecting you guys.

Cheers
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top