All instructions included in this document contain screenshots, making it easy for every Linux user to understand and apply them.”
Note: This article uses as example files older than 30 days, but you can change the number of days by replacing 30 with any number you want.
Deleting 30 Days Old Files in Linux
To begin, let’s see how to list files showing their date using the ls (List) command followed by the -l flag for long listing and by the path. In my case, the path is the LinuxHintDirectory directory located under the current directory.
As you can see in the figure above, there are two files and 2 directories older than 30 days. We can see files wpa.hash and wp-config.php, and wpascan and zmap directories.
The command below will remove files (No directories) in the specified path (As said, my path is LinuxHintDirectory located under the current path).
Note: In my case, I’m dealing with files created by the root user. Removing files massively as root is not recommended.
Note: You can replace +30 with +x (The number is arbitrary depending on your needs) to find files older than x. Or -x to find files newer than x. The differences are the plus and minus symbols.
As you can see in the previous image, files wpa.hash and wp-config.php were successfully deleted using the described command where:
sudo: This command gives privileges in case files you want to remove require superuser privileges. Using sudo when removing files is not the best option; I used it just for the examples.
find:
LinuxHintDirectory: In previous examples, LinuxHintDirectory is the directory where the files I want to remove are stored. This field must be replaced with the actual path to files you want to delete.
-type: This find command flag is used to define the type of file you want to remove (use an f for files and a d for directories).
f: After using the -type flag, the f, in this case, was used to specify we want to remove files except for directories.
-mtime: The -mtime flag is used to specify we want to find files according to their creation or modification time. This flag must be followed by a minus (-) and the number of days for created/modified files after the specified date. Or a plus symbol (+) followed by the number of days created or modified files older than X days ago.
+30: As said previously, the -mtime flag must be followed by the number of days according to which we want to find files. In this case, +30 specifies files created more than 30 days ago; you need to replace the number with the days you want to define as parameters.
-delete: This flag instructs the find command to delete files matching the previously described flags.
Just in case, I want to show another example showing a path with subdirectories, as you can see below, where the local LinuxHintDirectory is defined from the user’s home directory.
Now we can see in the screenshot below the LinuxHintDirectory location contains subdirectories emptyDir1, emptyDir2, linuxhintdir3, Templates, test, and touchp.
According to the dates shown below, the only eligible directories to be removed are linuxhintdir3, Templates, test, and touchp.
Now let’s see what happens when you replace -type f with -type d to specify directories.
As you can see below, from all directories matching the older than 30 days condition, only two were removed: linuxhintdir3 and Templates. This is because the previous command only removes empty directories. You can’ remove directories recursively using the command described above. That’s why you see errors in the last screenshot, warning it could not delete the non-empty listed directories.
To remove directories with content inside, we will combine the find command with the rm command. Before, let’s check the linuxhintdir directory for internal files and subdirectories by adding the –R (Recursive) flag as shown below.
As you can see above, all files and directories are older than 30 days. Therefore to remove all of them, I execute the following command as shown below.
As you can see, the directory linxhintdir was not removed, but all its content was removed. As you can see in the screenshot below, the reason behind linuxhintdir persistence is the directory was created today but contained old files.
Now let’s check the /opt directory recursively.
Note: I removed the google directory before this step.
Below you can see a full sequence. As you can see, there are two directories older than 30 days (gvm and teamviewer). Both of them have files and subdirectories. Let’s remove gvm and all its content by running the same command used previously, where we only change the path to the file (Now gvm).
As you can see, after running the command, only the teamviewer directory remains; gvm was successfully removed.
Conclusion
As you can see, removing files before or after a specific date is pretty simple. Any Linux user, independently of the knowledge level, must know how to find and manage files by date. The commands explained in this tutorial are valid for all Linux distributions. It is recommended for you to apply the above commands to gain practice (Use an experimental or testing environment). Remember, the + symbol means “more than” while the minus (-) symbol means “newer than”.
I hope this article explaining how to delete files older than 30 days in Linux was useful. Keep following us for more professional tutorials.