Delete files older than x days

There is always a requirement to delete files which are older than x days on Linux / Unix systems. Before we even start deleting some of these old files, let us first find out all the files that older than x days and save output into a file.

Find files older than x days

The below Linux command will help you to find files 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 specifies the modified time of a file. We are saving the list of all the files which are older than 35 days in backupfiles.log

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

Find files older than 7 days

If you want to print files 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

Find old files in current directory

Below linux command will find all the files under current location (as we have specified . dot), search file 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 {} \;

Was this article helpful?

Related Articles

Leave a Comment