Linux 下刪除大量檔案效率進行對比

2021-10-04 17:27:19 字數 1962 閱讀 3058

首先建立50萬個檔案

$ test

for i in

$(seq 1 500000);do

echo text >>

$i.txt;

done

1、rm 刪除

$ time

rm -f *

zsh: sure you want to delete all the files in /home/hungerr/test [yn]? y

zsh: argument list too long: rm

rm -f * 3.63s user 0.29s system 98% cpu 3.985 total

# 由於檔案數量過多,rm不起作用。

2、find 刪除

$ time

find ./ -type f -exec rm

\;find ./ -type f -exec rm

\; 49.86s user 1032.13s system 41% cpu 43:19.17 total#

3、find with delete

$ time

find ./ -type f -delete

find ./ -type f -delete 0.43s user 11.21s system 2% cpu 9:13.38 total

# 用時9分鐘。

4、rsync 刪除

# 首先建立空資料夾blanktest

$ time

rsync -a --delete blanktest/ test/

rsync -a --delete blanktest/ test/ 0.59s user 7.86s system 51% cpu 16.418 total16s,很好很強大。

5、python 刪除

import os

import timeit

defmain()

:for pathname,dirnames,filenames in os.walk(

'/home/username/test'):

for filename in filenames:

file

=os.path.join(pathname,filename)

os.remove(

file

)if __name__==

'__main__'

:t=timeit.timer(

'main()'

,'from __main__ import main'

)print t.timeit(1)

12$ python test.py

529.309022903

# 大概用時9分鐘。

6、perl 刪除

$ time perl -e 'for(<*>)'

perl -e 'for(<*>)' 1.28s user 7.23s system 50% cpu 16.784 total16s,這個應該最快了。

7、結果:

rm:檔案數量太多,不可用

find with -exec 50萬檔案耗時43分鐘

find with -delete 9分鐘

perl 16spython 9分鐘

rsync with -delete 16s

# 結論:刪除大量小檔案rsync最快,最方便。

Linux下刪除大量檔案效率對比

今天我們來測試一下linux下面刪除大量檔案的效率。首先建立50萬個檔案 test for i in seq 1 500000 do echo text i.txt done 1 rm刪除 time rm f zsh sure you want to delete all the files in ...

Linux下刪除大量檔案

主要參考了 首先建立50萬個檔案 test for i in seq 1500000 do echo text i.txt done test time rm f zsh sure you want to delete all the files in home hungerr test yn y ...

linux下快速刪除大量檔案

假如你要在linux下刪除大量檔案,比如100萬 1000萬,像 var spool clientmqueue 的mail郵件,像 usr local nginx proxy temp的nginx快取等,那麼rm rf 可能就不好使了。rsync提供了一些跟刪除相關的引數 rsync help gr...