How to delete files by age

Say your server is low on disk space and you have a large amount of old log files that can be deleted to free up some space. You only want to delete files older than i.e. 20 days.

$ find /path/to/logs/*.log -mtime +20 -exec rm -i {} \;

mtime +20 means that it should find only files that have not been modified within 20 days. This is quite a reasonable way to find log files. Considering you are deleting files, you can add the parameter -i to the command to get a prompt for each deletion, which is a safe way of ensuring that you are indeed deleting only the intended files.

How does the mtime options work?

mtime 20 means exactly 20 days ago. mtime +20 means more than 20 days, as in the example above. mtime -20 means less than 20 days ago.

Be Sociable, Share!

Related posts:

  1. How to find large files One of the beauties of Linux is that you can...