It is always recommended to find and cleanup your old files which are no longer necessary after a certain period of time. This will save you some disk space. If you didn't clean your old files yet, here is a quick way to do that. This brief tutorial walk you through how to find and delete files older than X days in Linux and Unix-like operating systems.
Disclaimer:
You should be extremely careful while running the following commands. These commands will not ask you any confirmation before deleting the files. It will simply delete the files once you hit the ENTER key. So be very careful and double check the files you're about to delete.
Table of Contents
Find and Delete Files Older than X days in Linux
First, let us find out the files older than X days, for example 30 days.
To do, so, just run:
$ find . -mtime +30 -print
The above command will find and display the older files which are older than 30 day in the current working directory.
In other words, this command performs a search for files in the current directory that have been modified at least 30 days ago and then prints the names of those files.
Here's a breakdown of the command:
find
: This is a command-line utility in Unix-based systems used to search for files and directories within a specified location.dot (.)
: Indicates the current directory.-mtime +30
: This is a parameter used to filter the search results based on the modification time of the files. In this case, it looks for files that were last modified more than 30 days ago. The+
sign indicates "greater than," so it finds files with a modification time of 30 days or older.-print
: This parameter instructs thefind
command to print the names of the files that match the specified criteria (in this case, files modified more than 30 days ago).
So, when you run this command, it will display the names of files in the current directory that were last modified at least 30 days ago.
If you want to search files in a specific directory, just replace the dot with the folder path.
For example, to find out the files which are older than 30 days in /home/sk/Downloads
directory, just run:
$ find /home/sk/Downloads -mtime +30 -print
Sample Output:
/home/sk/Pictures/Wallpapers /home/sk/Pictures/Wallpapers/29312450971_819c502b2e_o.jpg /home/sk/Pictures/Wallpapers/skamath-HD-Wallpapers1.jpg /home/sk/Pictures/Deepin Movie /home/sk/Pictures/smplayer_screenshots
Now, run any one of the following command to delete the files which are not required anymore. Again, I warn you that these commands will delete the files immediately once you hit ENTER button. Please be cautious and double check before running these commands.
$ find <Path_To_Old_Files> -type f -mtime +30 | xargs rm -f
Or,
$ find <Path_To_Old_Files> -mtime +30 -exec rm -f {} \;
Or,
$ find <Path_To_Old_Files> -mtime +30 -delete;
Bash Script to Delete Files Older than X Days
I have created a simple Bash script to find and remove files older than a certain period of time. This script will find and delete log files older than 30 days from the /var/log/
directory. You can edit the script and change the directory and time limit as you wish.
To run this script, simply copy and paste the above code in a text file, for example deloldfiles.sh
. Make the script executable using command:
chmod +x deloldfiles.sh
And run the script:
sudo ./deloldfiles.sh
This script warns the user about the consequences of running it, prompts the user if he/she wants to proceed. If the user type y
, it will proceed. If he type n
, it will exit.
Again, I warn you do not run this script without understanding it fully. You MUST change the location of the directory and the time interval before running it.
Conclusion
This is how I find and remove files older than certain period of time in my Linux servers. Delete old files periodically if they are not necessary at regular intervals, or backup them to any external drives and free up disk space. You can use the free space for any other useful purposes.
4 comments
You might to try tmpwatch
I prefer “find -ctime +30 -delete” over, sending it to exec, not sure if its specific to RHEL and CentOS 7 but it works fine in my testing.
> dot (.) – Indicates the current directory.
> -mtime – Indicates the file modification time and is used to find files older than 30 days.
> -print – Displays the older files
what I missed and needed in the explanation was + , the thing that actually makes that it takes older files.
Thanks for the command, nevertheless.
I somehow missed it. Now added the description of each part in the command. Hope it makes sense now. Thanks for pointing it out.