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

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.

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

Linux operating systems are widely used for web servers and other data-heavy tasks such as penetration testing etc. In these applications, there can be an accumulation of files that are not useful after a certain period. Now Linux, being an operating system used mostly for its automation functionalities, provides many ways to remove files older than a particular number of days automatically. In this article, we shall see ways to remove files older than x days automatically.

Similar Reads

Pre-requisites

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

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

Step 1: Using the find 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....

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

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

Conclusion

In this article, we learned how to use the find command to search for files older than x 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:...

Contact Us