Quick Linux Tip:
If you’re trying to delete a very large number of files at one time (I deleted a directory with 485,000+ today), you will probably run into this error:
The problem is that when you type something like “rm -rf *”, the “*” is replaced with a list of every matching file, like “rm -rf file1 file2 file3 file4″ and so on. There is a reletively small buffer of memory allocated to storing this list of arguments and if it is filled up, the shell will not execute the program.
To get around this problem, a lot of people will use the find command to find every file and pass them one-by-one to the “rm” command like this:
My problem is that I needed to delete 500,000 files and it was taking way too long.
I stumbled upon a much faster way of deleting files – the “find” command has a “-delete” flag built right in! Here’s what I ended up using:
Using this method, I was deleting files at a rate of about 2000 files/second – much faster!
You can also show the filenames as you’re deleting them:
…or even show how many files will be deleted, then time how long it takes to delete them:
real 0m3.660s
user 0m0.036s
sys 0m0.552s
« Flex 2 DataGrid ItemRenderers are re-used! Warning!!! Bidirectional LocalConnections in Actionscript 3 (Flex 2 / Flex 3 / Flash CS3) »


Brilliant! Works like a CHAMP. Total lifesaver. We are using this to delete over 8 million cache files from a series of directories. Quick tip, using the first letter of a portion of the files seems to make it run faster. In our example, every filename is a random hexadecimal string. We know that the filenames will randomly begin with 0-9 or a-f. We are running 16 commands like the following:
find a* -type f -print -delete
Oh, and for those who may not know, the ‘-type f’ is for files and ‘-type d’ is for directories.
Hi Todd, I’m glad it worked for you!
Please note that in the form of the command you posted:
The a* will be expanded by the shell into a list of all the files that begin with a. It will also fail if there are too many files beginning with that letter/number. If this happens, you need to put the a* in quotes like this, to let the find command evaluate this shell expansion:
Oh perfect, I noticed that I was still getting the ‘Argument list too long.’ error. I will finish deleting the files using your suggestion.
Thanks again!!
I am getting a “No such file or directory” error with the single quotes around the file mask.
Oh, sorry, here’s the correct syntax:
Note: the ‘.’ is the path to look for the files in.
Thanks for the article. Helped me much.
I just deleted 1.3 million files with this command: find . -type f -delete
It took about 10 seconds.
Total lifesaver! Watch where you use it though. This little nugget of information is a jewel that I will NEVER forget. Thanks!!
Perfect. Thanks for the advice. Was trying to work out how to delete Magento’s stupidly massive session storage (740k files). Will have to cron job that folder to stop that happening in the future.
Thanks, I now will delete so many files, that I can’t even count, cos server freezes ^^