If you’re trying to lớn delete a very large number of files at one time (I deleted a directory with 485,000+ today), you will probably run rẩy into this error:
/bin/rm: Argument list too long.
The problem is that when you type something lượt thích rm -rf *
, the *
is replaced with a list of every matching tệp tin, lượt thích “rm -rf file1 file2 file3 file4” and so sánh on. There is a relatively small buffer of memory allocated to lớn 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 lớn find every tệp tin and pass them one-by-one to lớn the “rm” command lượt thích this:
find . -type f -exec rm -v {} \;
My problem is that I needed to lớn 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:
find . -type f -delete
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:
find . -type f -print -delete
…or even show how many files will be deleted, then time how long it takes to lớn delete them:
root@devel# ls -1 | wc -l && time find . -type f -delete
100000
real 0m3.660s
user 0m0.036s
sys 0m0.552s