How to delete files older than 7 days in Linux :

delete files older than 7 days in Linux
As a DBA or Linux Admin, its required to know as a part of daily tasks how to delete files older than 7 days in Linux. This can be any number of days or time. Today we will discuss on this with the commands.

List files older than 7 days in Linux :

Go to the particular directory and issue the below command to list all the files which are older than 7 days

find . -name "*.trc" -type f -mtime +7 -exec ls -ltr {} \;

Go to the particular directory and issue the below command to list all the files that are older than 7 mins

find . -name "*.trc" -type f -mmin +7 -exec ls -ltr {} \;

Issue the below command to list all the files that are older than 7 days under the directory ‘/u01/backup’

find /u01/backup -type f -mtime +7 -exec ls -ltr {} \;

Delete files older than 7 days in Linux :

Go to the particular directory and issue the below command to delete files that are older than 7 days

find . -name "*.trc" -type f -mtime +7 -exec rm {} \;

Go to the particular directory and issue the below command to delete all the files that are older than 7 mins

find . -name "*.trm" -type f -mtmin +7 -exec rm {} \;

Issue the below command to list all the files that are older than 7 days under the directory ‘/u01/backup/trace’

find /u01/backup/trace -type f -mtime +7 -exec ls -ltr {} \;
In case of folder deletion,

Delete folders older than 7 days in Linux :

Issue the below command to delete all the folders that are older than 7 days under the directory ‘/u01/backup/’

find /u01/backup -type d -mtime +7 -exec rm -rf {} \;

So, here are the detailed steps to delete files older than 7 days in Linux. You can do same for folder too by the last step. Also, you can replace the number of days or time you want as per your requirement.

Our other related Linux Related Articles :

Or, you can get further reference from Redhat Doc also.

Leave a Reply