Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


if(somedir2) contains file with extension .txt or .log 
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
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

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

Your Answer


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.