How To Find And Delete Files Older Than X Days In Linux

Find And Delete Files Older Than X Days In Linux

It is very important to find and cleanup your old files which are no longer necessary after a certain period of time. 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.

Find and Delete Files Older Than X Days In Linux

Disclaimer: You must be extremely careful while running these commands. These commands doesn’t ask you any confirmation before deleting the files. It will simply delete the files once you hit the ENTER key. So be very careful!

First, let us find out the files older than X days, for example 30 days.

To do, so, just:

find . -mtime +30 -print

The above command will find and display the older files which are older than 30 days. Here,

  • dot (.) – Represents the current directory.
  • -mtime – Represents the file modification time and is used to find files older than 30 days.
  • -print – Displays the older files

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

sksk_001

Now, to delete the files, run any one of the following command. 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;

Also Read: Find And Delete Oldest File If There Are More Than X Files In A Directory


That’s it. 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.

If you find this guide useful, please share it on your social networks and spread the word about our site.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • H0rnet

    You might to try tmpwatch