Deleting many files from the Linux Command Line

I’ll admit that this post is more for me than any of my readers. I have this command that is buried in my notes and always takes me forever to dig back out. I figured I’d publish it on my blog so that I would maybe commit it to memory.

Let’s say that you have a directory with so many files that a simple “rm *” will always fail. I’ve encountered this with many WordPress logging plugins that don’t have log purging setup.

Enter this simple Linux command line command:

find <path> -type f -exec rm '{}' \;

What this will do is find all the files in your path and delete them. You can modify this command with a bunch of other flags like:

find <path> -type f -mtime 30 -exec rm '{}' \;

Which will only delete files that haven’t been modified in the last 30 days.

I’m sure there are many other flags and conditions you could check to create an even more fine-grained delete script but this has been useful for me!

If this helps you, please share this with your friends!


Posted

in

,

by

Tags:

Comments

Leave a Reply