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

2022-03-22 06:45:30 字數 1798 閱讀 7640

今天我們來測試一下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 /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刪除

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 total

16s,很好很強大。

5、python刪除

1

importos2

import

timeit

3def

main():

4for pathname,dirnames,filenames in os.walk('

/home/username/test'):

5for filename in

filenames:

6 file=os.path.join(pathname,filename)

7os.remove(file)89

if__name__=='

__main__':

10 t=timeit.timer('

main()

','from __main__ import main')

11print t.timeit(1)

$ 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 total

16s,這個應該最快了。

7、結果:

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

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

find with -delete 9分鐘

perl 16s

python 9分鐘

rsync with -delete 16s

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

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

首先建立50萬個檔案 test for i in seq 1 500000 do echo text i.txt done1 rm 刪除 time rm f zsh sure you want to delete all the files in home hungerr test yn y zsh...

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...