欢迎大家来到IT世界,在知识的湖畔探索吧!
CentOS系统环境下,单一目录小文件数量超过20万,使用系统默认的rm工具去执行删除,会报-bash: /usr/bin/rm: Argument list too long,无法正常执行删除操作;需要选择合理的删除方式,下面是常用的单目录大量小文件删除方式的效率对比。

欢迎大家来到IT世界,在知识的湖畔探索吧!
测试是在虚拟环境下,测试数据仅供参考,选取的数据量分别是30万和50万,验证哪种删除方式更高效。测试使用的工具包括rm命令,find+delete命令,find+exec命令,find+xargs命令,rsync命令。
采用脚本生成小文件
#生成30万个4K小文件 seq |xargs -i dd if=/dev/zero of=/home/test30w_1/{}.txt bs=4096 conut=1 #生成50万个4K小文件 seq |xargs -i dd if=/dev/zero of=/home/test50w_1/{}.txt{}.txt bs=4096 conut=1 #统计生成文件数量 ls /home/test30w_1/ |wc -l;ls /home/test50w_1/ | wc -l #复制数据副本用于测试 cp -r /home/test30w_1 /home/test30w_2
欢迎大家来到IT世界,在知识的湖畔探索吧!
单目录30万数据测试场景:
欢迎大家来到IT世界,在知识的湖畔探索吧!#准备测试数据 [root@localhost test30w_1]# ls | wc -l [root@localhost test30w_1]# cd .. [root@localhost home]# ls test30w_1 [root@localhost home]# cp -r test30w_1 test30w_2 [root@localhost home]# cp -r test30w_1 test30w_3 [root@localhost home]# cp -r test30w_1 test30w_4 [root@localhost home]# cp -r test30w_1 test30w_5 [root@localhost home]# cp -r test30w_1 test30w_6 [root@localhost home]# ls test30w_1 test30w_2 test30w_3 test30w_4 test30w_5 test30w_6 [root@localhost home]# #rm删除文件目录方式耗时测试 [root@localhost home]# time rm -rf test30w_6 real 0m26.279s user 0m0.123s sys 0m5.882s #rm删除文件列表方式,直接报错参数列表过长 [root@localhost home]# time rm -rf test30w_5/* -bash: /usr/bin/rm: Argument list too long real 0m1.727s user 0m1.611s sys 0m0.102s #find+exec方式,耗时7分48秒 [root@localhost home]# time find test30w_5/ -type f -exec rm -rf {} \; real 7m48.080s user 1m8.101s sys 6m46.340s #find+delete方式,耗时11秒 [root@localhost home]# time find test30w_4/ -type f -delete real 0m11.918s user 0m0.217s sys 0m9.408s # find+xargs方式,耗时16秒 [root@localhost home]# time find test30w_3/ -type f -print0 |xargs -0 rm -rf real 0m16.084s user 0m0.501s sys 0m11.739s # find+xargs方式,配置批量参数传输,耗时17秒 [root@localhost home]# time find test30w_2/ -type f -print0 |xargs -0 -n 50000 rm -rf real 0m17.737s user 0m0.499s sys 0m11.988s # find+xargs方式,配置2并发和批量参数传输,耗时15秒 [root@localhost home]# time find test30w_1/ -type f -print0 |xargs -0 -P 2 -n 50000 rm -rf real 0m15.714s user 0m0.566s sys 0m14.755s [root@localhost home]#
单目录50万数据测试场景
#测试数据准备 [root@localhost home]# ls test50w_1/ |wc -l [root@localhost home]# du -sh * 2.0G test50w_1 2.0G test50w_2 2.0G test50w_3 2.0G test50w_4 #rm删除目录方式耗时32秒 [root@localhost home]# time rm -rf test50w_1 real 0m32.750s user 0m0.325s sys 0m16.982s #find+delete方式耗时17秒 [root@localhost home]# time find test50w_2/ -type f -delete real 0m17.049s user 0m0.384s sys 0m15.823s #find+xargs方式耗时18秒 [root@localhost home]# time find test50w_3/ -type f -print0 | xargs -0 rm -rf real 0m18.032s user 0m0.790s sys 0m15.746s #rsync空文件夹同步方式耗时24秒 [root@localhost home]# time rsync -a --delete --progress /tmp/test/ test50w_5/ >/dev/null real 0m24.861s user 0m1.012s sys 0m18.696s #find+xargs方式,采用4进程加批量参数提交耗时33秒 [root@localhost home]# time find test50w_6/ -type f -print0 | xargs -0 -P 4 -n 60000 rm -rf real 0m33.597s user 0m1.096s sys 0m37.608s
单目录数据量在50万小文件以下的,建议使用find + delete模式进行删除,50万数据量以上的,建议选择rsync或者find+xargs组合方式删除,能够提高删除效率。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/123539.html