您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

linux技巧:一次删除一百万个文件的最快方法

2024/6/19 20:48:39发布23次查看
最初的测评
昨天,我看到一个非常有趣的删除一个目录下的海量文件的方法。这个方法来自http://www.quora.com/how-can-someone-rapidly-delete-400-000-files里的zhenyu lee。
他没有使用find 或 xargs,他很有创意的利用了rsync的强大功能,使用rsync –delete将目标文件夹以一个空文件夹来替换。之后,我做了一个实验来比较各种方法。让我吃惊的是,lee的方法要比其它的快的多。下面就是我的测评。
环境:
cpu: intel(r) core(tm)2 duo cpu e8400 @ 3.00ghz
mem: 4g
hd: st3250318as: 250g/7200rpm
method
# of files
deletion time
rsync -a –delete empty/ s1/    1000000    6m50.638s    
find s2/ -type f -delete    1000000    87m38.826s    
find s3/ -type f | xargs -l 100 rm    1000000    83m36.851s    
find s4/ -type f | xargs -l 100 -p 100 rm    1000000    78m4.658s    
rm -rf s5    1000000    80m33.434s    
使用 –delete 和 –exclude,你可以选择性删除符合条件的文件。还有一点,当你需要保留这个目录做其它用处时,这种方法是再适合不过了。
重新测评
几天前,keith-winstein在回复quora上的这个帖子时说我之前的测评无法复制,因为操作的时间持续的太久。我澄清一下,这些数据过大,可能是因为我的计算机在过去的几年里做的事太多,测评中可能存在一些文件系统错误。但我不确定是这些原因。现在好了,我弄了一天比较新的计算机,把测评再做一次。这次我使用/usr/bin/time,它能提供更详细的信息。下面就是新的结果。
(每次都是1000000个文件。每个文件的体积都是0。)
command
elapsed
system time
%cpu
cs (vol/invol)
rsync -a –delete empty/ a    10.60    1.31    95    106/22    
find b/ -type f -delete    28.51    14.46    52    14849/11    
find c/ -type f | xargs -l 100 rm    41.69    20.60    54    37048/15074    
find d/ -type f | xargs -l 100 -p 100 rm    34.32    27.82    89    929897/21720    
rm -rf f    31.29    14.80    47    15134/11    
原始输出
# method 1 ~/test $ /usr/bin/time -v rsync -a --delete empty/ a/ command being timed: "rsync -a --delete empty/ a/" user time (seconds): 1.31 system time (seconds): 10.60 percent of cpu this job got: 95% elapsed (wall clock) time (h:mm:ss or m:ss): 0:12.42 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 0 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 24378 voluntary context switches: 106 involuntary context switches: 22 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0 # method 2 command being timed: "find b/ -type f -delete" user time (seconds): 0.41 system time (seconds): 14.46 percent of cpu this job got: 52% elapsed (wall clock) time (h:mm:ss or m:ss): 0:28.51 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 0 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 11749 voluntary context switches: 14849 involuntary context switches: 11 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0 # method 3 find c/ -type f | xargs -l 100 rm ~/test $ /usr/bin/time -v ./delete.sh command being timed: "./delete.sh" user time (seconds): 2.06 system time (seconds): 20.60 percent of cpu this job got: 54% elapsed (wall clock) time (h:mm:ss or m:ss): 0:41.69 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 0 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 1764225 voluntary context switches: 37048 involuntary context switches: 15074 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0 # method 4 find d/ -type f | xargs -l 100 -p 100 rm ~/test $ /usr/bin/time -v ./delete.sh command being timed: "./delete.sh" user time (seconds): 2.86 system time (seconds): 27.82 percent of cpu this job got: 89% elapsed (wall clock) time (h:mm:ss or m:ss): 0:34.32 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 0 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 1764278 voluntary context switches: 929897 involuntary context switches: 21720 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0 # method 5 ~/test $ /usr/bin/time -v rm -rf f command being timed: "rm -rf f" user time (seconds): 0.20 system time (seconds): 14.80 percent of cpu this job got: 47% elapsed (wall clock) time (h:mm:ss or m:ss): 0:31.29 average shared text size (kbytes): 0 average unshared data size (kbytes): 0 average stack size (kbytes): 0 average total size (kbytes): 0 maximum resident set size (kbytes): 0 average resident set size (kbytes): 0 major (requiring i/o) page faults: 0 minor (reclaiming a frame) page faults: 176 voluntary context switches: 15134 involuntary context switches: 11 swaps: 0 file system inputs: 0 file system outputs: 0 socket messages sent: 0 socket messages received: 0 signals delivered: 0 page size (bytes): 4096 exit status: 0
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product