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:
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.
Contact Us