Thursday, May 5, 2011

Deleting hundereds of thousand of files in Linux.

So, it turns out that in Linux the rm command can't handle deleting huge numbers of files.  In my case, I wanted to delete 400,000 files from a directory because said directory had used up all the available inodes on that drive.

Trying to do an rm -rf * would produce an "argument list too long" error.  So I searched around and found that you can do this from within the offending directory:

find . -name '*' | xargs rm

Which essentially finds and deletes each file individually, running rm over and over again.  It didn't even take THAT long to run (5-10 minutes or so).

Remember to change to the offending directory first though, this can be pretty destructive if run on the wrong directory.

No comments: