昨天遇到一個問題,在Linux中有一個文件夾裏面含有大量的Cache文件(夾),數量級可能在百萬級別,使用rm -rf ./*
刪除時間慢到不可接受。 Google了一下,查到了一種方法,試用了下確實比單純使用rm快了一個數量級。linux
1)首先創建一個空白文件夾。bash
mkdir /tmp/empty
複製代碼
2)以後使用如下語句便可快速的刪除文件。ide
rsync --delete-before -d /tmp/empty/ /the/folder/you/want/delete/
複製代碼
原文中說不要忘記文件夾後的 /
ui
rsync
提供了一些跟刪除相關的參數spa
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實際上用的是替換原理,處理數十萬個文件也是秒刪。code
選項說明:遞歸