How to Find & Delete Files in Directory by Modification Date in Linux

How to find and delete files in a Linux directory based on when the file had been modified from a certain amount of days ago – this can be useful for dealing with directories with copious amounts of files like email boxes and log folders.

First to see the files modified or created based on a certain date:

find /path-to-directory -mtime +5 -exec ls -l {} \;

This will list all the files created or modified 5 days prior to the current date.

Then to remove the files:

find /path-to-directory -mtime +5 -exec rm -f {} \;

This will remove all those files previously listed.

Alter the ‘-mtime’ argument in the command to change the amount of days, by adjusting the figure after it. ‘+5’ will preserve any files created/modified withiin the last 5 days and delete files modified older then 5 days ago.

1 Comment

  1. 2linuxdz on May 7, 2021 at 1:21 pm

    useful tutorial

Leave all Comment