Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Setting up a cron job for once a month to start a script.

the script is to rotate a file each month as it gets too big and rename the old one. when a file is older than six months I wanted it to be deleted.

Looking to run this script once a month. Is that possible to get the old one deleted?

Please let me know if that isn't clear enough.

share|improve this question
10  
Why not just use logrotate? –  Michael Hampton May 29 '13 at 20:33
    
@scottmarriott yeah, Mr Hampton's right, the functionality you described is basically the exact thing logrotate does. –  Bratchley May 29 '13 at 20:45
    
Yeah I've just had a good look at logrotate makes sense cheers all –  scott marriott May 29 '13 at 20:54
    
@MichaelHampton you should make that an answer. –  evilsoup May 30 '13 at 18:53

2 Answers 2

You could start with this:

find /your/file -mtime +182 -exec rm {} +

Where +182 are the days quantity.

share|improve this answer
    
Thanks for that, might have a play with it, but I maybe logrotate might be the best option –  scott marriott May 29 '13 at 20:55
    
@jordanm any ideas why this might not of worked? –  scott marriott May 30 '13 at 9:51
    
ApplicationServer/logs/TestClone01 $ ls -ltr total 3320 1048458 13 Dec 09:46 SystemOut_12.12.13_09.46.29.log 28 May 12:44 SystemOut.log.2013-05-28-12:44:52 71 30 May 08:57 SystemOut.log.2013-05-30-08:57:22 48432 30 May 09:16 SystemOut.log no_env):/usr/websphere6/was61TE_LV2/ApplicationServer/logs/TestClone01 $ find SystemOut.log -mtime +1 -exec rm {} + find: 0652-083 Cannot execute :: A file or directory in the path name does not exist. –  scott marriott May 30 '13 at 9:53
    
I changed number of days to 1 –  scott marriott May 30 '13 at 10:09
    
@scottmarriott Not all versions of find support +/- for -mtime. What OS and version of find do you have? –  jordanm May 30 '13 at 13:04

You can just use logrotate. It's already available on most Linux systems, and many packages already have logrotate scripts pre-configured, or you can tweak them or write your own.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.