Bootstrap

How to automatically delete/remove files older than x days in Linux

Pre-requisites

Before proceeding with the tutorial, make sure that you fulfill the following requirements:

  1. Linux Machine.
  2. Knowledge of Linux Terminals.
  3. Understanding of cronjobs.

Use the Find command with xargs to delete files older than x days

Step 1: Using the find command

Now, to get files files based on certain criteria, we have the find command in Linux. The find command searches for a file(s) in a directory hierarchy. This means that if you are searching for a file say file1.sh in a folder say my_folder then, the find command will search for this file in the directory my_folder and all the sub-folders present in that directory.

Syntax:

find <search directory> <search criteria>

Here, the first argument is for the search directory. If left empty then, find searches in the current working directory. The second argument decides the search criteria and has many different options, which we will use in the later steps.

Step 2: Getting files that are older than x days

Now, to find files that are older than x days, meaning they were last modified x days ago, we will use the find command. The syntax to do the same is:

Syntax:

find <search directory> -type f -mtime +x
  • find: The first option for the find command is the path to the search directory, if no path is passed it works on the present working directory.
  • -type: The next component is the -type option. This option specifies what kind of object we are searching for. For example, is used for all general file types such as text files, scripts, etc. The d type searches for directories only. Here we are looking for files only so, we use the option with -type.
  • mtime: The last component is the mtime component which takes input the number of days, the searched object was last modified. In this case, we looking for files older than days so, we passed +x, where the + suffix indicates that the time could be or more but, not less.

Let us see the same in action. For illustration purposes, we will take files modified five days ago or older. We are using the following directory structure for this example:

-/
    -home
        -my_folder
            -file1
            -file2
            -file3

Here, we are in the /home folder, inside which we create a dummy folder my_folder containing 3 files last modified 5 days ago. We shall now try to use the find command to search for these files.

The command is the same as explained above. We have just passed the search directory and number of days. The output of this command is:

find /home/my_folder -type f -mtime +5

Output:

Output of find command

Output of find command

Now, as we can see the files are found.

Step 3: Changing the output to argument form

Before we can move further and delete these files, we have a slight syntactical problem. The rm command that removes files takes input in argument form i.e., separated by a blank space but, in the output of the find command, we got file names separated by a newline character, which is not supported by rm. Therefore, we need to change the list of file names into argument form.

Fortunately, there is a simple command, the xargs command, that converts lines of text from stdin(standard input) into an argument list.

Syntax:

xargs <command>

The command option takes the command that needs to be executed with the positional arguments given by xargs. First, let us see how xargs changes the output of our previous step.

find /home/my_folder/ -type f -mtime +5 | xargs

Output:

Output of find command in argument form

Output of find command in argument form

Here, we passed the output of the find command to the xargs command using the pipe command (|).

Step 4: Removing the files with rm

Now that we have got the files that we need to remove, that too in arguments form, all that remains is to remove these files. This can be achieved by simply passing the rm command to the xargs command in the previous step.

Note: You must be very cautious when removing files using the rm and with sudo privileges. One wrong execution can permanently delete some system files which will ruin your OS. Due to this very reason, all the tasks in this article are performed in a common non / folder.

This will delete all the files in the my_folder directory, which are older than 5 days.

find /home/my_folder -type f -mtime +5 | xargs rm

Removing files older than 5 days and verifying the same with ls

Removing files older than 5 days and verifying the same with ls

You can also verify whether the files have been deleted or not using the ls command.

Using the ‘find’ command with the -exec option

In the previous method, we had to use an external command, the xargs command to change the output of the find command in a pipable form to the rm command. However, there exists another way to run the rm command with the output of the find command without using the xargs command; using the -exec option of the find command.

The -exec option allows the user to run a command with the output of the find command. The syntax is as follows:

Syntax:

find <other options> -exec <command> {} \;

Here,

  • <command>: The <command> placeholder takes the command to be executed.
  • :{}: The {} placeholder specifies the path files found by the find command.
  • \: The indicates the end of the -exec option.
  • ;: Theis required to indicate the end of the command as the terminal by default takes the as a newline character.

To use this to find and delete files older than x days (5 days in this example) we can change the command from method 1 to the following:

find /home/my_folder -type f -mtime +5 -exec rm {} \;

Output:

Removing files with the -exec option in the find command.

Removing files with the -exec option in the find command.

As we can see the files that were older than 5 days were removed from the specified folder.

Using the ‘find’ command with -delete option

There is another way to use the find command for deleting files older than x days, by using the inbuilt -delete option. This option deletes the files found by the find command. This is the most effective way to use the find command for deleting files as it uses a single command rather than multiple commands. The syntax of the same is given below.

Syntax:

find <other options> -delete

You only have to add the -delete option at the end of your find command and it will delete the files found based on the criteria given in the command. In our example, we can modify the find command as follows to delete files older than 5 days:

find /home/my_folder -type f -mtime +5 -delete 

Output:

Deleting files using the -delete option in the find command

Deleting files using the -delete option in the find command

Scheduling the auto-deletion task with cronjob

Now that we have learned how to remove files older than x days. We will now set up a cronjob to execute the command daily to make the task automatic, which is the objective of this article. In case you are not familiar with cronjobs, refer to this article. We shall not explain them in detail here as it is not in this article’s scope.

The cronjobs required to automate the above methods to run daily are as follows:

Example 1:
0 0 * * * find /home/my_folder -type f -mtime +5 | xargs rm


Example 2:
0 0 * * * find /home/my_folder -type f -mtime +5 -exec rm {} \;

Example 3:
0 0 * * * find /home/my_folder -type f -mtime +5 -delete

Here, the first two arguments ‘0 0‘ refers to the minute and hour of the day. We have set it to midnight but, you can choose any time of your choosing. The next three arguments ‘ * * * ‘ represent day-of-month, the month of the year, and day-of-week respectively. We have set them to * which means on every occurrence of these times. Then, we finally give the command we want to execute.

Now, you only need any one of the methods to complete the deletion task. You can choose any of your liking and then, to make its cronjob work, you need to append this to our Linux machine’s crontable which can be accessed by following the command:

crontab -e

If you are using crontab for the first time, you will be asked to choose your preferred editor from your installed editors. Once the selection is done, you will have your crontable file as follows:

Output:

Crontable

Crontable

Now, all that is left is to add the cronjob created above at the end of this file without a suffix. After that, you just save the changes and exit the editor.

Now, you have successfully set up automatic removal of files older than x days. A thing to note here is that we have specified the folder here, from which we need to remove x days older files.

Note: One must never set up a cronjob for a current folder of crontable or any other system file folder unless they are aware of what they are doing otherwise, they may end up corrupting their system.

Conclusion

In this article, we learned how to use the find command to search for files older than days in a specified folder and then, we learned 4 three different methods to use the find command to remove those files. We used the following methods:

  1. find command with xargs and rm command.
  2. find command with -exec option to remove files with the rm command.
  3. find command with -delete option to delete files without any other command.
  4. Scheduling the auto-deletion task with cronjob
;