How to delete files older than X days in Linux

Find les older than X days and save ouput into a file

The below Linux command will help you to nd les older than 35 days in a specific directory path and save the ouput in backupfiles.log

Here the directory we are searching is /backup/logs and -mtime speci es the modi ed time of a le. We are saving the list of all the les which are older than 35 days in backupfiles.log

find /backup/logs -type f -mtime +35 -print > backupfiles.log &

 

Find les older than 7 days and print output on screen

If you want to print les older than 7 days on screen and do not want to save it into a file, use below command

find /backup/logs -type f -mtime +7 -print

 

READ ALSO
How to set DISPLAY variable in Linux

Find les in current directory older than 28 days and remove them

Below linux command will nd all the les under current location (as we have speci ed . dot), search le name starting with arch and ending with log. check file create time with -ctime older than 28 days and then remove those files using rm -f

find . -name arch\*log -ctime +28 -exec rm -f {} \;

 

 

Leave a Reply

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