1. Overview

We often need to tidy up files on our workstations or servers. Our applications may produce logs or temporary files. One common use case is to delete files that are above a certain age.

In this tutorial, we’ll look at ways to delete files by age on Linux. These commands may also work in other POSIX shells.

2. Finding and Deleting Files

We’re going to need to find the files that match our criteria in order to apply the delete action.

For this, we’ll use the find command. The find command even provides a delete capability that we can use.

2.1. Delete Files Older Than X Minutes

Let’s start by using find to delete files whose file names start with access and end with .log, and which are older than 15 minutes:

find . -name "access*.log" -type f -mmin +15 -delete

Let’s have a closer look at how this command is constructed.

First, we’ve specified the starting point for files lookup, it’s the current working directory “.”.

Then, we have the file name criteria prefixed with the -name switch.

The switch -type f means we want to look for files only.

The -mmin stands for the modification time, and +15 means we want files that were last modified 15 minutes ago or earlier.

The action flag -delete asks find to delete all the files it finds. We should note that this will look recursively through the file system hierarchy, starting from the current working directory.

2.2. Delete Files Older Than X Days

It only takes a small change to the find command to switch from minutes to days:

find . -name "access*.log" -type f -mtime +5 -delete

Here, the -mtime switch says we want to delete files that were modified at least 5 days ago.

2.3. Delete Files Older Than X Days With an Older Version of find

Using older distributions, the find tool might not have the -delete switch.

In this instance there’s another way:

find . -name "access*log" -exec rm {} \;

In this version, the -exec switch allows us to use the rm command on each file found.

2.4. Delete Files Older Than X Days With a Prompt

We might be concerned that an incorrectly constructed delete command might end up deleting the wrong files. A small variation of the above command will prompt us before deletion.

Let’s add the -i switch to the rm command:

find . -name "access*log" -exec rm -i {} \;

This way, we can decide which files get deleted.

3. Avoiding Accidental File Deletion

Deleting files is fairly easy, but we must remember we’re doing it for all the files that match the find predicate. This means a simple typo or unexpected order of the command line switches might cause unexpected damage.

As an example, let’s look at the following command:

find . -delete -name file.txt

We might assume that this would delete only file.txt from the current working directory. However, since the -delete switch comes first, the -name is ignored. This mistake will delete everything in our current directory!

Here are some general rules we should follow to improve safety when deleting with find:

  • Make sure the find command is correct by previewing it, running it without the -delete switch
  • Always check the -delete option is at the end of the find arguments
  • Never delete the files as the root user unless absolutely necessary

4. Summary

In this tutorial, we’ve looked at how we can delete files older than some period of time.

Next, we looked at what might go wrong if given switches are out of the necessary order.

Finally, we’ve had a brief look at general rules we should follow when running such commands on our system.

The find command is very handy and has lots of additional switches. We can find out more about it with either man find or find –help.

Comments are closed on this article!