Linux上删除大量文件几种方式对比

时间:2022-10-15 00:22:24

Linux上删除大量文件几种方式对比

linux上删除大量小文件删除测试:删除500000个小文件测试

生成文件:

$ for i in $(seq 500000);do echo 'text' >hello/$i.txt;done
$ du -sh hello
2.0G hello

1. rm删除:因为文件数量太多,rm无法删除(报错)

$ time rm -f *
-bash: /usr/bin/rm: Argument list too long real 0m2.676s
user 0m2.340s
sys 0m0.272s

2. find查找删除:-exec

-exec: 该参数是将查找的结果文件名,逐个传递给后面的命令执行,如果文件较多,会导致效率较低。

xargs: 该命令是将查找的结果一次性传给后面的命令执行,效率比较高。但是不能操作文件名有特殊字符的文件。

$ time find . -type f -exec rm {} \;

real	7m16.154s
user 0m51.171s
sys 5m51.214s

3. find查找删除:xargs

$ time find . -type f|xargs rm -rf

real	0m17.860s
user 0m1.021s
sys 0m8.838s

4. find调用-delete删除

$ time find . -type f -delete

real	0m16.953s
user 0m0.535s
sys 0m8.866s $ time find . -type f -delete real 0m16.221s
user 0m0.578s
sys 0m8.098s

5. ls和xargs组合删除

$ time ls|xargs -L 10000 rm -f

real	0m13.764s
user 0m2.856s
sys 0m8.630s $ time ls|xargs -L 5000 rm -f real 0m13.879s
user 0m3.053s
sys 0m8.622s

6. rsync delete删除

--delete delete extraneous files from dest dirs
--delete-before receiver deletes before xfer, not during
--delete-during receiver deletes during the transfer
--delete-after receiver deletes after transfer, not during
$ mkdir blank # 建立空文件夹
$ time rsync -a --delete blank/ hello/ real 0m15.293s
user 0m0.590s
sys 0m12.397s $ time rsync -a --delete blank/ hello/ real 0m14.833s
user 0m0.590s
sys 0m11.977s $ time rsync -a --delete-before blank/ hello/ real 0m14.884s
user 0m0.602s
sys 0m12.147s $ time rsync -a --delete-during blank/ hello/ real 0m15.468s
user 0m0.693s
sys 0m12.441s $ time rsync -a --delete-after blank/ hello/ real 0m15.135s
user 0m0.600s
sys 0m12.253s

总结:

命令 时间 备注
rm 无法删除 ...
find + -exec 7m16s 耗费时间太长
find + xargs 17s 时间较短
find + delete 16s 时间较短
ls + xargs 13s 时间短
rsync + delete 15s 时间短