日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

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

發(fā)布時間:2023/12/13 linux 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux技巧:一次删除一百万个文件最快方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

昨天,我看到一個非常有趣的刪除一個目錄下的海量文件的方法。這個方法來自http://www.quora.com/How-can-someone-rapidly-delete-400-000-files里的Zhenyu Lee。

他沒有使用findxargs,他很有創(chuàng)意的利用了rsync的強大功能,使用rsync –delete將目標(biāo)文件夾以一個空文件夾來替換。之后,我做了一個實驗來比較各種方法。讓我吃驚的是,Lee的方法要比其它的快的多。下面就是我的測評。

環(huán)境:

  • CPU: Intel(R) Core(TM)2 Duo CPU E8400 @ 3.00GHz
  • MEM: 4G
  • HD: ST3250318AS: 250G/7200RPM
Method# Of FilesDeletion Time
rsync -a –delete empty/ s1/10000006m50.638s
find s2/ -type f -delete100000087m38.826s
find s3/ -type f | xargs -L 100 rm100000083m36.851s
find s4/ -type f | xargs -L 100 -P 100 rm100000078m4.658s
rm -rf s5100000080m33.434s

?

使用 –delete–exclude,你可以選擇性刪除符合條件的文件。還有一點,當(dāng)你需要保留這個目錄做其它用處時,這種方法是再適合不過了。

重新測評

幾天前,Keith-Winstein在回復(fù)Quora上的這個帖子時說我之前的測評無法復(fù)制,因為操作的時間持續(xù)的太久。我澄清一下,這些數(shù)據(jù)過大,可能是因為我的計算機在過去的幾年里做的事太多,測評中可能存在一些文件系統(tǒng)錯誤。但我不確定是這些原因?,F(xiàn)在好了,我弄了一天比較新的計算機,把測評再做一次。這次我使用/usr/bin/time,它能提供更詳細(xì)的信息。下面就是新的結(jié)果。

(每次都是1000000個文件。每個文件的體積都是0。)

CommandElapsedSystem Time%CPUcs (Vol/Invol)
rsync -a –delete empty/ a10.601.3195106/22
find b/ -type f -delete28.5114.465214849/11
find c/ -type f | xargs -L 100 rm41.6920.605437048/15074
find d/ -type f | xargs -L 100 -P 100 rm34.3227.8289929897/21720
rm -rf f31.2914.804715134/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.31System time (seconds): 10.60Percent of CPU this job got: 95%Elapsed (wall clock) time (h:mm:ss or m:ss): 0:12.42Average shared text size (kbytes): 0Average unshared data size (kbytes): 0Average stack size (kbytes): 0Average total size (kbytes): 0Maximum resident set size (kbytes): 0Average resident set size (kbytes): 0Major (requiring I/O) page faults: 0Minor (reclaiming a frame) page faults: 24378Voluntary context switches: 106Involuntary context switches: 22Swaps: 0File system inputs: 0File system outputs: 0Socket messages sent: 0Socket messages received: 0Signals delivered: 0Page size (bytes): 4096Exit status: 0# method 2Command being timed: "find b/ -type f -delete"User time (seconds): 0.41System time (seconds): 14.46Percent of CPU this job got: 52%Elapsed (wall clock) time (h:mm:ss or m:ss): 0:28.51Average shared text size (kbytes): 0Average unshared data size (kbytes): 0Average stack size (kbytes): 0Average total size (kbytes): 0Maximum resident set size (kbytes): 0Average resident set size (kbytes): 0Major (requiring I/O) page faults: 0Minor (reclaiming a frame) page faults: 11749Voluntary context switches: 14849Involuntary context switches: 11Swaps: 0File system inputs: 0File system outputs: 0Socket messages sent: 0Socket messages received: 0Signals delivered: 0Page size (bytes): 4096Exit status: 0 # method 3 find c/ -type f | xargs -L 100 rm ~/test $ /usr/bin/time -v ./delete.shCommand being timed: "./delete.sh"User time (seconds): 2.06System time (seconds): 20.60Percent of CPU this job got: 54%Elapsed (wall clock) time (h:mm:ss or m:ss): 0:41.69Average shared text size (kbytes): 0Average unshared data size (kbytes): 0Average stack size (kbytes): 0Average total size (kbytes): 0Maximum resident set size (kbytes): 0Average resident set size (kbytes): 0Major (requiring I/O) page faults: 0Minor (reclaiming a frame) page faults: 1764225Voluntary context switches: 37048Involuntary context switches: 15074Swaps: 0File system inputs: 0File system outputs: 0Socket messages sent: 0Socket messages received: 0Signals delivered: 0Page size (bytes): 4096Exit status: 0# method 4 find d/ -type f | xargs -L 100 -P 100 rm ~/test $ /usr/bin/time -v ./delete.shCommand being timed: "./delete.sh"User time (seconds): 2.86System time (seconds): 27.82Percent of CPU this job got: 89%Elapsed (wall clock) time (h:mm:ss or m:ss): 0:34.32Average shared text size (kbytes): 0Average unshared data size (kbytes): 0Average stack size (kbytes): 0Average total size (kbytes): 0Maximum resident set size (kbytes): 0Average resident set size (kbytes): 0Major (requiring I/O) page faults: 0Minor (reclaiming a frame) page faults: 1764278Voluntary context switches: 929897Involuntary context switches: 21720Swaps: 0File system inputs: 0File system outputs: 0Socket messages sent: 0Socket messages received: 0Signals delivered: 0Page size (bytes): 4096Exit status: 0# method 5 ~/test $ /usr/bin/time -v rm -rf fCommand being timed: "rm -rf f"User time (seconds): 0.20System time (seconds): 14.80Percent of CPU this job got: 47%Elapsed (wall clock) time (h:mm:ss or m:ss): 0:31.29Average shared text size (kbytes): 0Average unshared data size (kbytes): 0Average stack size (kbytes): 0Average total size (kbytes): 0Maximum resident set size (kbytes): 0Average resident set size (kbytes): 0Major (requiring I/O) page faults: 0Minor (reclaiming a frame) page faults: 176Voluntary context switches: 15134Involuntary context switches: 11Swaps: 0File system inputs: 0File system outputs: 0Socket messages sent: 0Socket messages received: 0Signals delivered: 0Page size (bytes): 4096Exit status: 0

我真的十分好奇為什么Lee的方法要比其它的快,竟然比rm -rf也要快。如果有人知道,請寫在下面,非常感謝。

[英文原文: A faster way to delete millions of files in a directory ]

轉(zhuǎn)載于:https://www.cnblogs.com/hubing/p/3194378.html

總結(jié)

以上是生活随笔為你收集整理的Linux技巧:一次删除一百万个文件最快方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。