HOWTO Delete Old Directory in UNIX

There are 2 ways u can do this. For example if u want to delete directory older than 7 days :

First method :
[text]
find /path/dir -type d -mtime +7 -exec rm -rf {} ;
[/text]

Second method :
[text]
find /path/dir -type d -mtime +7 | xargs rm -rf {} ;
[/text]

You can always change the +7 value to any number of days preferred.
FYI, this command is only for directory deletion, if u want to delete files, u can change the “-type d” (d means directory) to “-type f” (f means files).

Example :
[text]
find /path/dir/unl.txt -type f -mtime +7 | xargs rm -f {} ;
[/text]

P/S : There’s an alternative command which we can use, tmpwatch (more info : http://linux.about.com/library/cmd/blcmdl8_tmpwatch.htm)

Leave a Reply

Your email address will not be published. Required fields are marked *