Linux技巧:一次刪除一百萬個(gè)文件的最快方法
最初的測(cè)評(píng)
昨天,我看到一個(gè)非常有趣的刪除一個(gè)目錄下的海量文件的方法。這個(gè)方法來自http://www.quora.com/How-can-someone-rapidly-delete-400-000-files里的Zhenyu Lee。
他沒有使用find 或 xargs,他很有創(chuàng)意的利用了rsync的強(qiáng)大功能,使用rsync –delete將目標(biāo)文件夾以一個(gè)空文件夾來替換。之后,我做了一個(gè)實(shí)驗(yàn)來比較各種方法。讓我吃驚的是,Lee的方法要比其它的快的多。下面就是我的測(cè)評(píng)。
環(huán)境:
- CPU: Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
- MEM: 4G
- HD: ST3250318AS: 250G/7200RPM
使用 –delete 和 –exclude,你可以選擇性刪除符合條件的文件。還有一點(diǎn),當(dāng)你需要保留這個(gè)目錄做其它用處時(shí),這種方法是再適合不過了。
重新測(cè)評(píng)
幾天前,Keith-Winstein在回復(fù)Quora上的這個(gè)帖子時(shí)說我之前的測(cè)評(píng)無法復(fù)制,因?yàn)椴僮鞯臅r(shí)間持續(xù)的太久。我澄清一下,這些數(shù)據(jù)過大,可能是因?yàn)槲业挠?jì)算機(jī)在過去的幾年里做的事太多,測(cè)評(píng)中可能存在一些文件系統(tǒng)錯(cuò)誤。但我不確定是這些原因?,F(xiàn)在好了,我弄了一天比較新的計(jì)算機(jī),把測(cè)評(píng)再做一次。這次我使用/usr/bin/time,它能提供更詳細(xì)的信息。下面就是新的結(jié)果。
(每次都是1000000個(gè)文件。每個(gè)文件的體積都是0。)
原始輸出
# 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
我真的十分好奇為什么Lee的方法要比其它的快,竟然比rm -rf也要快。如果有人知道,請(qǐng)寫在下面,非常感謝。
[英文原文: A faster way to delete millions of files in a directory ]