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

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

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


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 0

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