Debian – How to delete files older than X days

This post is also available in: Romanian

Hey, recently I have wondered what is the CLI command that I have to use in order to delete all the files older than X days. Using a debian server I have to use a linux command to do that.

I have discovered that I have to use the “find” command to filter those files first and then select them to delete.

Command Sintax:

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

This searches for files older than 5 days in the mentioned folder and delete them all.

Explanation

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 should work on Ubuntu, Suse, Redhat, or pretty much any version of linux.

Leave a Reply

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

CAPTCHA * Time limit is exhausted. Please reload CAPTCHA.