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 :
find /path/dir -type d -mtime +7 -exec rm -rf {} ;

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

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 :
find /path/dir/unl.txt -type f -mtime +7 | xargs rm -f {} ;

P/S : There’s an alternative command which we can use, tmpwatch (more info :

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑