Find (and delete) old files in Linux

close up view of system hacking
Photo by Tima Miroshnichenko on Pexels.com

I’ve got a confession to make – I’m a log file addict. I love a good log file and I’ll hold on to those bad boys for way longer than I should sometimes. Even I need to clear down the odd /logs directory every now and then though, and that’s where this handy little piece of command-line-fu comes into play.

find . -type f -mtime +90 

As you might have guessed, this finds all the files in the current directory that are over 90 days old. You can change the +90 to any other value to tighten or widen your search.

find . -type f -mtime +90 -delete

This version, with the addition of -delete automatically deletes the files that it finds. Use with caution; there’s no confirmation and no going back. Always run the find without -delete first to check what you are about to get rid of.

Be the first to comment

Leave a Reply