Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

How can I delete all files that are older than one year from a certain directory within the bash?


share|improve this question
up vote 7 down vote accepted
find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

vi samefilename 

find /u1/database/prod/arch -type f -mtime +3 -exec rm {} \;

The only 2 commands used are find and rm.

Find looks for files (-type f), this to exclude directories, that are older then 3 days (-mtime +3). All it finds is given to rm (-exec rm {} \; ).

You could also place the rm statement outside of find, which is supposed to be faster:

find /u1/database/prod/arch -type f -mtime +3 | xargs rm

share|improve this answer
You can use + instead of \; with find for perfomance similar to xargs. – Dennis Williamson Jul 26 '10 at 14:07
Also, when using xargs, please do it as find … -print0 | xargs -0 … (if available) so that pathnames with spaces (and even embedded line feed characters) are handled correctly. – Chris Johnsen Jul 26 '10 at 15:48
that is very good suggestion fro me thanks ton!! – Rajat Jul 26 '10 at 15:51

another approach I found. nice for specific dates.

touch --date="2010-1-1" x
find -not -newer x|xargs rm
share|improve this answer

tmpwatch does a good job, example:

/usr/sbin/tmpwatch $[24*365] /tmp 

Exerpt from manual:

tmpwatch recursively removes files which haven’t been accessed for a given number of hours. Normally, it’s used to clean up directories which are used for temporary holding space such as /tmp.

tmpwatch [-u|-m|-c] [-MUadfqstvx] [--verbose] [--force] [--all]
                  [--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet]
                  [--atime|--mtime|--ctime] [--dirmtime] [--exclude <path>]
                  [--exclude-user <user>] <hours> <dirs>
share|improve this answer

tmpwatcher or "tmpreaper" in ubuntu.

use with -c

share|improve this answer

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.