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.

I am new to shell scripting. I am trying to write a cron for unix shell that will delete some files, say after 2 weeks or after 1 month from a specific directory.

/somedir1/somedir2/

if(somedir2) contains file with extension .txt or .log 
then
check timestamp
if two weeks old delete it
otherwise don't delete.

Any help in this regard will be appreciated.

share|improve this question
    
The find command does everything you need. man find –  Mikel Aug 27 '12 at 2:32
add comment

2 Answers 2

up vote 5 down vote accepted

Try the find command.

find /somedir1/somedir2 -name *.txt -name *.log -mtime 2w -delete

Change -delete to -print for a dry run.

share|improve this answer
add comment

You could also leverage tmpwatch http://linux.die.net/man/8/tmpwatch, using whichever parameters of atime, ctime, or mtime suit your needs.

For example, tmpwatch --atime 30d /foo will remove all files in /foo that haven't been accessed in 30 days.

share|improve this answer
    
Thanks for your answer. –  Nomad Aug 29 '12 at 19:04
add comment

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.