How to Unix tip: Find and remove files older than a certain time period


Unix Command line

If you find a directory such as /tmp/ filling up with old and uneeded files, here’s a quick tip for finding and removing what you don’t need anymore. Note that the delete function WILL remove everything it finds, so please understand exactly what it is you are doing here before running that command.

Command to find all files older than 5 days:
find . -mtime +5 -type f;

Command to delete all files older than 5 days (USE WITH CARE!):
find . -mtime +5 -type f -exec rm {} \;

From the man page for find:

-mtime n
     File’s data was last modified n*24 hours ago. See the comments
     for -atime to understand how rounding affects the interpretation
     of file modification times.

Hope that helps! Thanks for the pointers from this page.


Leave a Reply

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

sell diamonds