How to find and delete files older than x days on linux

To locate and delete files older than x days, you should use combination of find and rm commands. In this example, I will use 5 days:

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

Explanation:

– The first argument is basically the path to files. It can be a directory or whole mount (partition)
– The next argument -mtime is used to specify the number of days to differ from the date of file modification
– The last argument -exec allows us to pass the next command rm {} \;

*Please note that there is space between rm, {} and \;

Alternative method would be using xargs argument:

find /path/to/files -mtime +5 | xargs rm -fr

Leave a Reply

Your email address will not be published. Required fields are marked *

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