How to use the ‘find’ command with -delete option In Linux

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

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