Linux find and remove files older than N days

Find files older than N days

If you wish to find and delete certain files that are older than a certain amount of days, you can use this snippet:

find /path/to/files-beginning-with* -mtime +5

What this does is it finds all files matching the filter /path/to/files-beginning-with* that have been created more than 5 days ago. You can change the “+5” to any amount of days you would like.

If you execute this command, you will get a list of all the files matching this filter.

Find and delete files

If you wish to delete all files that have been found, you can append -exec rm {} \; to the command. The full command will look like this:

find /path/to/files-beginning-with* -mtime +5 -exec rm {} \;

By Leendert de Borst

Freelance software architect with 10+ years of experience. Expert in translating complex technical problems into creative & simple solutions.

Leave a comment

Your email address will not be published.