How to find and delete files older than x days

Sometimes, when we have few space on server or need to delete log files older than 7 days, and when you don’t know which folder(s) have those older files, you fell the necessity of a command to delete those files.

So, imagine if you have got one folder for logs with 10 folders in it. Those tenĀ folders, have subfolders in them and those subfolders have subfolders too, and so on. With this, i want to delete all the files older than 7 days in all folders including subfolders without touching folder structure, that means only files.What i need to do?

Using command find you’ll be able to identify each file older than 7 days and delete it.

The typical actions to do this are:

This will run rm -rf on each file

-exec rm -rf {} \;

This will run rm -f on many files at once

-exec rm -f {} +;

to complete, the final command will be like this:

find /var/dtpdev/tmp/ -type f -mtime +7 -exec rm -f {} \;

And that’s it. Hope it helps you.


No Comments

Post a Comment