Home Command line utilities How To Find And Delete Files Older Than X Days In Linux
Find And Delete Files Older Than X Days In Linux

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

By sk
14844 Views

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.

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 directorys.

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

find files older than 30 days

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;

Also Read: 


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.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

H0rnet December 7, 2016 - 12:04 am

You might to try tmpwatch

Reply
JonTheWong November 5, 2019 - 1:33 am

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.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More