• ARTICLES
SEARCH

How-To Geek

Delete Files Older than a Certain Date

Have you ever needed to clean out some old files out of a folder? This is especially useful when you are trying to keep a log directory from getting too bloated with files, and Linux makes it really easy to clear out the junk.

We’ll just use the find command to search for all the files older than a certain date, and then use the –exec command to delete them. You could use the –delete command instead, but you’ll end up with a maximum arguments when there are too many files.

Find Command Syntax

find /path/to/files* -mtime +5 -exec rm {} ;

Note that there are spaces between rm, {}, and ;

Explanation for Command

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} ; at the end is required to end the command.

This pretty much works on any ‘nix distribution.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/21/09

Enter Your Email Here to Get Access for Free:

Go check your email!