Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

Thanks!

share|improve this question

4 Answers 4

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

vi samefilename 
#!/bin/bash

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
3  
You can use + instead of \; with find for perfomance similar to xargs. –  Dennis Williamson Jul 26 '10 at 14:07
4  
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

tmpwatcher or "tmpreaper" in ubuntu.

http://linux.about.com/library/cmd/blcmdl8_tmpwatch.htm

use with -c

share|improve this answer

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

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.