在Linux下使用 "rm -rf *" 试图删除非常大量的小文件时,可能会报类似下边的错误:
/bin/rm: Argument list too long.
这是因为通配符"*"在执行时会被每个匹配的文件名来替换,例如“rm -rf file1 file2 file3 file4″。
系统只分配了一个比较小的缓冲区用来对这个列表进行排序,如果这个缓冲区满了,则shell不能执行这个命令。
为了解决这个问题,很多人使用find命令来操作。即用find查找每一个文件,然后把文件名一个接一个的传递个rm命令,形式如下:
find . -type f -exec rm -v {} \;
但是这个方法对于数量巨大的文件时非常缓慢。
google找到了这份blog文章: Deleting tons of files in Linux (Argument list too long),参考文中的方法解决了我的问题。
办法就是使用find命令内置的 "-delete" 参数 ,使用这种方法删除文件,速度大概是 2000个文件/秒,速度比之前方法得到极大提高。当然也可以加上 "-print"参数来显示每个删除的文件。
find . -type f -delete
find . -type d -print -delete
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 delete them:
root@devel# ls -1 | wc -l && time find . -type f -delete
100000
real 0m3.660s
user 0m0.036s
sys 0m0.552s
转载于:https://blog.51cto.com/xoyabc/1660685