Today I needed to delete a bunch of old log files to clear up some room on our server. Here’s a handy shortcut that I found to delete any files that are older than a given number of days old.

First you’ll want to run this command to make sure you’re selecting just the files you want. It’s best to use the full path so there’s no mistake!

sudo find /path/to/dir -type f -mtime +7

Once you’re sure you want to proceed, you can add the remove command to the end like this:

sudo find /path/to/dir -type f -mtime +7 -exec rm {} \;

Here’s an explanation of each part:

  • sudo – Perform the action as if you were root (avoids permission denied errors)
  • find /path/to/dir – Find files in this directory path
  • -type f – Only get files of type “file” (excludes directories)
  • -mtime +7 – Only get files whose timestamp is greater than 7 days old
  • -exec rm {} \; – Perform the “rm” action on the results