Try it here
Subscribe
find, mtime, rm and delete

Find and Delete Files Older Than X Days In Linux

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.

  • 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/de/Downloads directory, just run:

$ find /home/de/Downloads -mtime +30 -print

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;

Writer profile pic

Neha on Sep 27, 2020 at 12:09 am


This article is contributed by Neha. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.