如何快速的删除大量文件

参考链接:

  http://unix.stackexchange.com/questions/96935/faster-way-to-delete-large-number-of-files

  http://www.slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux

试验场景:生成5000个1MB大小的文件,然后用find删除。

看结果说话:

[user@host test]$ for x in `seq 1000`; do dd if=/dev/zero of=$x.dd count=1 bs=1MB 2>/dev/null; done
[user@host test]$ time find . -type f -name "*.dd" -exec rm -rf {} \;

real    0m1.590s
user    0m0.120s
sys     0m1.125s
[user@host test]$ 
[user@host test]$ for x in `seq 1000`; do dd if=/dev/zero of=$x.dd count=1 bs=1MB 2>/dev/null; done
[user@host test]$ time find . -type f -name "*.dd" -delete 

real    0m0.387s
user    0m0.002s
sys     0m0.385s
[user@host test]$ 
[user@host test]$ for x in `seq 1000`; do dd if=/dev/zero of=$x.dd count=1 bs=1MB 2>/dev/null; done
[user@host test]$ time find . -type f -name "*.dd" -exec rm -rf {} + 

real    0m0.387s
user    0m0.001s
sys     0m0.385s
[infa@wltscj1 test]$ 
[infa@wltscj1 test]$ for x in `seq 5000`; do dd if=/dev/zero of=$x.dd count=1 bs=1MB 2>/dev/null; done
[infa@wltscj1 test]$ time find . -type f -name "*.dd" -delete 

real    0m2.176s
user    0m0.014s
sys     0m2.140s
[user@host test]$ 
[user@host test]$ for x in `seq 5000`; do dd if=/dev/zero of=$x.dd count=1 bs=1MB 2>/dev/null; done
[user@host test]$ time find . -type f -name "*.dd" -exec rm -rf {} +

real    0m2.038s
user    0m0.012s
sys     0m2.020s
[user@host test]$ 

结论,下面这种写法效率相对较高些:

find [OPTIONS] -exec rm {} +

 

更好的方式:

#创建空白目录
[user@host ~]$ mkdir blank/
#删除test目录下的所有文件:
[user@host ~]$ time rsync -a --delete-before blank/ test/


real    0m1.906s
user    0m0.010s
sys     0m1.838s
[user@host ~]$

 

posted @ 2014-11-07 13:49  lichmama  阅读(698)  评论(0编辑  收藏  举报