0
votes

How can I delete all files that are older than one year in a specific folder with the bash under Linux?

4 Answers 4

7
votes
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

2
  • 3
    You can use + instead of \; with find for perfomance similar to xargs. Jul 26, 2010 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. Jul 26, 2010 at 15:48
1
vote

another approach I found. nice for specific dates.

touch --date="2010-1-1" x
find -not -newer x|xargs rm
1
vote

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>
0
votes

tmpwatcher or "tmpreaper" in ubuntu.

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

use with -c

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .