Delete Files Older Than x Days on Linux

Delete Files Older Than x Days on Linux

The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

Command Syntax

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

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


  • 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.


Changing the file dates in Ubuntu

You can change the modification time of a file using the touch command:

touch filename

By default this will set the file’s modification time to the current time, but there are a number of flags, such as the -d flag to pick a particular date. So for example, to set a file as being modified two hours before the present, you could use the following:

touch -d "2 hours ago" filename

If you want to modify the file relative to its existing modification time instead, the following should do the trick:

touch -d "$(date -r filename) - 2 hours" filename

If you want to modify a large number of files, you could use the following:

find DIRECTORY -print | while read filename; do
    # do whatever you want with the file
    touch -d "$(date -r "$filename") - 2 hours" "$filename"

You can change the arguments to find to select only the files you are interested in. If you only want to update the file modification times relative to the present time, you can simplify this to:

find DIRECTORY -exec touch -d "2 hours ago" {} +

This form isn’t possible with the file time relative version because it uses the shell to form the arguments to touch.

As far as the creation time goes, most Linux file systems do not keep track of this value. There is actime associated with files, but it tracks when the file metadata was last changed. If the file never has its permissions changed, it might happen to hold the creation time, but this is a coincidence. Explicitly changing the file modification time counts as a metadata change, so will also have the side effect of updating the ctime.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s