Linux下快速刪除大量檔案

2021-09-08 17:03:59 字數 1319 閱讀 2472

昨天遇到乙個問題,在linux中有乙個資料夾裡面含有大量的cache檔案(夾),數量級可能在百萬級別,使用rm -rf ./* 刪除時間慢到不可接受。google了一下,查到了一種方法,試用了下確實比單純使用rm快了乙個數量級。

方法見下:

1)首先建立乙個空白資料夾。

mkdir /tmp/empty

2)之後使用以下語句即可快速的刪除檔案。

rsync --delete-before -d /tmp/empty/ /the/folder/you/want/delete/

原文中說不要忘記資料夾後的「/」

rsync提供了一些跟刪除相關的引數 

rsync --help | grep delete 

--del                   an alias for --delete-during 

--delete                delete files that don't exist on the sending side 

--delete-before         receiver deletes before transfer (default) 

--delete-during         receiver deletes during transfer, not before 

--delete-after          receiver deletes after transfer, not before 

--delete-excluded       also delete excluded files on the receiving side 

--ignore-errors         delete even if there are i/o errors 

--max-delete=num        don't delete more than num files 

rsync --delete-before -a -h -v --progress --stats /tmp/test/ log/

這樣我們要刪除的log目錄就會被清空了,刪除的速度會非常快。rsync實際上用的是替換原理,處理數十萬個檔案也是秒刪。

選項說明

–delete-before 接收者在傳輸之前進行刪除操作

–progress 在傳輸時顯示傳輸過程

-a 歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性

-h 保持硬連線的檔案

-v 詳細輸出模式

–stats 給出某些檔案的傳輸狀態

linux下快速刪除大量檔案

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

linux下快速刪除大量檔案

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

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