在Linux系統中大量刪除多個檔案的方法總結

來源:互聯網
上載者:User

當我們在linux系統中要刪除數萬或者數十萬甚至數百萬的檔案時使用rm -rf *就不太好用,因為要等待很長一段時間。在這種情況之下我們可以使用linux系統命令rsync來巧妙的處理。rsync實際上用的是替換原理,處理數十萬個檔案也是秒刪。
1. rsync安裝,有些系統預設安裝有該命令
ubuntu系統:

代碼如下:
sudo apt-get install rsync

fedora 系統:

代碼如下:
sudo yum install rsync

其他的可以源碼安裝,到下面的網站下載
http://rsync.samba.org

2. rsync提供了一些跟刪除有關的參數
rsync --help | grep delete
     --del                                an alias for --delete-during
     --delete                          delete extraneous files from destination dirs
     --delete-before             receiver deletes before transfer, not during
     --delete-during             receiver deletes during transfer (default)
     --delete-delay               find deletions during, delete after
     --delete-after                receiver deletes after transfer, not during
     --delete-excluded        also delete excluded files from destination dirs
     --ignore-errors             delete even if there are I/O errors
     --max-delete=NUM    don't delete more than NUM files
其中--delete-before接收者在傳輸之前進行刪除操作

3. 樣本
清空目錄或檔案,如下:
1、先建立一個空目錄

代碼如下:
mkdir /data/blank

2、用rsync刪除目標目錄

代碼如下:
rsync --delete-before -d -a -H -v --progress --stats /data/blank/ /var/edatacache/

或者

代碼如下:
rsync --delete-before -d /data/blank/ /var/edatacache/

這樣/var/edatacache目錄就被快速的清空了。

選項說明:
–delete-before 接收者在傳輸之前進行刪除操作
–progress          在傳輸時顯示傳輸過程
-a                       歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性
-H                      保持硬串連的檔案
-v                       詳細輸出模式
–stats                給出某些檔案的傳輸狀態
-d                      transfer directories without recursing

刪除檔案夾
如果大量小檔案都集中在幾個目錄中,那麼這個方法就有用了。

代碼如下:
rm -f /var/log/httpd/access.log

將會強制移除/var/log/httpd/access.log這個檔案
-r 就是向下遞迴,不管有多少級目錄,一併刪除
-f 就是直接強行刪除,不作任何提示的意思
-i 進行互動式刪除。
提示:使用rm命令要小心。因為一旦檔案被刪除,它是不能被恢複的。了防止這種情況的發生,可以使用i選項來逐個確認要刪除的檔案。如果使用者輸入y,檔案將被刪除。如果輸入任何其他東西,檔案則不會刪除。
使用這個rm -rf刪除檔案的時候一定要格外小心,linux沒有資源回收筒的。
rm -r 目錄名:
將子目錄及子目錄中所有檔案刪除

代碼如下:
[root@localhost test]# ls -l

總計 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 2 root root 4096 10-26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5

代碼如下:
[root@localhost test]# rm -r test1

rm:是否進入目錄 “test1”? y
rm:是否刪除 一般檔案 “test1/log3.log”? y
rm:是否刪除 目錄 “test1”? y

代碼如下:
[root@localhost test]# ls -l

總計 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#
rm -rf 目錄名 :
把子目錄及子目錄中所有檔案刪除,並且不用一一確認 

代碼如下:
[root@localhost test]# rm -rf test2
[root@localhost test]# ls -l

總計 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.