Delete only files older than 7 days: -mtime and find

find

Found a few similar questions but were not quite a match.

I have a directory for backups (sql) and want to delete all files in that directory older then 7 days leaving any sub-directories intact.

This is what I have:

find /var/log/mbackups -mtime +7 -type f -delete

Is this the proper way to accomplish what I am after?

Best Answer

Your command will look at the top level directory /var/log/mbackups and also descend into any subdirectories, deleting files that match the seven day criterion. It will not delete the directories themselves.

If you want a command to look at files only in the /var/log/mbackups directory, and not descend into subdirectories, you need to add that restriction:

find /var/log/mbackups -maxdepth 1 -mtime +7 -type f -delete

In general you can test the find command by replacing the -delete action with something innocuous, like -print:

find /var/log/mbackups -mtime +7 -type f -print
Related Question