10 Linux rsync 命令排除文件/目錄

Rsync(遠程同步)是 Linux/Unix 系統中最常用的命令,用於遠程和本地複制和同步文件和目錄。 借助 rsync 命令,您可以跨目錄、跨磁盤和網絡遠程和本地複制和同步數據,在兩台 Linux 機器之間執行數據備份和鏡像。

Rsync 是服務器管理員最有用的實用程序之一,但默認情況下它會同步所有內容,如果您的應用程序創建了大量臨時文件,這可能會很煩人。 以下是使用 rsync 時排除文件的方法。

rsync 命令的重要特性

  • 速度 :第一次,rsync 在源目錄和目標目錄之間複製整個內容。 下一次,rsync 僅將更改的塊或字節傳輸到目標位置,這使得傳輸速度非常快。
  • 安全 : rsync 允許在傳輸過程中使用 ssh 協議加密數據。
  • 低帶寬: rsync 在發送端和接收端分別對數據進行逐塊壓縮和解壓。 因此,與其他文件傳輸協議(如 FTP)相比,rsync 使用的帶寬總是更少。
  • 特權 : 安裝和執行 rsync 不需要特殊權限。

rsync 的基本語法非常簡單,其操作方式類似於 ssh、scp 和 cp。

$ sudo rsync options source destination

  • -v :冗長。
  • -r :遞歸複製數據(但在傳輸數據時不保留時間戳和權限)。
  • -a :歸檔模式,歸檔模式允許遞歸複製文件,它還保留符號鏈接、文件權限、用戶和組所有權和時間戳。
  • -z :壓縮文件數據。
  • -h :人類可讀的,以人類可讀的格式輸出數字。
  • -d:不遞歸地傳輸目錄 -e:將 ssh 指定為遠程 shell。

首先,創建一個可用於測試目的的示例目錄結構,如下所示(帶有一些空文件)。

$ cd ~
$ sudo mkdir -p source/dir1/dir2
$ sudo mkdir -p source/dir3
$ sudo touch source/file1.txt
$ sudo touch source/file2.txt
$ sudo touch source/dir1/dir2/file3.txt
$ sudo touch source/dir3/file4.txt

上述命令將創建一個具有以下結構的源目錄(在主目錄下),要查看源目錄類型下面的命令的結構。

$ sudo find source/

source
- file1.txt
- file2.txt
- dir1
- dir2
- file3.txt
- dir3
- file4.txt

1) 排除特定目錄

首先我們刪除 destination 目錄(如果存在),請鍵入以下命令。

$ sudo rm -rf destination

如果我們不想同步 dir3 及其從源文件夾到目標文件夾的子目錄,我們使用 rsync --exclude 選項如下圖。

$ sudo rsync -avz --exclude 'dir3' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude 'dir3' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt

sent 307 bytes received 126 bytes 866.00 bytes/sec
total size is 0 speedup is 0.00

2)排除特定文件

如果我們不想同步 file3.txt 從源到目標文件夾,我們鍵入下面的命令。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude 'dir1/dir2/file3.txt' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude 'dir1/dir2/file3.txt' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir3/
dir3/file4.txt

sent 338 bytes received 130 bytes 936.00 bytes/sec
total size is 0 speedup is 0.00

為了 example 如果您希望將 /var 實時同步到備份目錄,但又不希望包含通常在重新啟動之間不保存重要內容的緩存和 tmp 文件夾,您可以使用以下命令:

$ sudo rsync -aAXhv --exclude={"/var/cache","/var/tmp"} /var [email protected]:destination/

3) 排除特定的文件類型

為了排除具有特定擴展名的特定對象,我們使用適當的模式。 為了 example, 排除所有包含 .txt 作為擴展,我們輸入下面的命令。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude '*.txt' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude '*.txt' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/
dir3/

sent 136 bytes received 65 bytes 402.00 bytes/sec
total size is 0 speedup is 0.00

4) 排除多個匹配模式的目錄

我們將排除下的任何目錄(或子目錄) source/ 以“dir”開頭

$ sudo rm -rf destination

$ sudo rsync -avz --exclude 'dir*' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ $ sudo rsync -avz --exclude 'dir*' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt

sent 168 bytes received 91 bytes 518.00 bytes/sec
total size is 0 speedup is 0.00

5)同時​​排除多個文件和目錄

首先,創建一個文本文件,其中包含您不想備份的所有文件和目錄的列表。 這是要從 rsync 中排除的文件和目錄列表。

$ sudo nano exclude-list.txt

file2.txt
dir3/file4.txt

接下來,使用執行 rsync --exclude-from 選項與 exclude-list.txt 如下所示。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude-from 'exclude-list.txt' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude-from 'exclude-list.txt' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir3/

sent 277 bytes received 111 bytes 776.00 bytes/sec
total size is 0 speedup is 0.00

6)排除路徑總是相對的

exclude 選項似乎有一個完整路徑(即 /dir3/file4.txt)。 但是,從 rsync 的角度來看,排除路徑始終是相對的,它將被視為 dir3/file4.txt。 在裡面 example 下面,rsync 將在源目錄下(而不是在 / 根目錄下)查找 dir3。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude '/dir3/file4.txt' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude '/dir3/file4.txt' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/file3.txt
dir3/

sent 334 bytes received 130 bytes 928.00 bytes/sec
total size is 0 speedup is 0.00

它類似於下面的命令。

$ sudo rsync -avz --exclude 'dir3/file4.txt' source/ destination/

7) 從命令行中排除文件

也可以直接從命令行排除文件,當您要排除的文件數量較少時,這很有用。 為了 example 如果我們希望將 /source 同步到目標目錄,但您不希望包含 file3.txt 和 file4.txt 文件,我們可以使用以下命令。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude={"/dir1/dir2/file3.txt","/dir3/file4.txt"} source/ destinaton/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude={"/dir1/dir2/file3.txt","/dir3/file4.txt"} source/ [email protected]:destination/

output
sending incremental file list
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir3/

sent 268 bytes received 73 bytes 682.00 bytes/sec
total size is 0 speedup is 0.00

8) 排除多個文件或文件夾

當我們想排除文件類型時 *.txt 和特定文件夾 dir3,我們可以在下面輸入命令。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude '*.txt' --exclude 'dir3' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude '*.txt' --exclude 'dir3' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/

sent 109 bytes received 61 bytes 340.00 bytes/sec
total size is 0 speedup is 0.00

9) 排除具有特定模式的多個目錄

我們還可以提供具有特定排除模式的多個目錄。 我們將使用通配符 * 去完成 dir 目錄名。

$ sudo rm -rf destination

$ sudo rsync -avz --exclude 'dir*' source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --exclude 'dir*' source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt

sent 168 bytes received 91 bytes 518.00 bytes/sec
total size is 0 speedup is 0.00

10) 排除某些大小的文件

我們在備份過程中遇到一些大文件需要很長時間才能複制。 我們必須複製一些大小超過 1GB 的電影。 所以,我們認為如果我們可以簡單地排除大文件或任何不重要的文件以便盡快完成備份會更好。

為了 example,我們要排除大於 3 MB 的文件。 我們所要做的就是使用 --max-size=SIZE rsync 命令的選項。 此選項不會傳輸任何大於指定大小的文件。

$ sudo rm -rf destination

$ sudo rsync -avz --max-size=3m source/ destination/

對於遠程同步,我們可以在下面輸入命令。

$ sudo rsync -avz --max-size=3m source/ [email protected]:destination/

output
sending incremental file list
created directory destination
./
file1.txt
file2.txt
dir1/
dir1/dir2/
dir1/dir2/Bootstrap-tutorial.pdf
dir1/dir2/file3.txt
dir3/
dir3/file4.txt

sent 3,080,297 bytes received 168 bytes 6,160,930.00 bytes/sec
total size is 8,101,480 speedup is 2.63

我們也可以使用 --min-size=SIZE 排除任何小於指定大小的文件。

為了 example, 要排除小於 7 MB 的文件,請運行以下命令。

$ sudo rsync -avz --min-size=7m source/ destination/

output
sending incremental file list
created directory destination
./
dir1/
dir1/dir2/
dir3/

sent 313 bytes received 65 bytes 756.00 bytes/sec
total size is 8,101,480 speedup is 21,432.49

尺寸的後綴如下:

  • “K”(或“KiB”)是千字節 (1024)
  • “M”(或“MiB”)是兆字節 (1024*1024)
  • “G”(或“GiB”)是千兆字節(1024*1024*1024)

如果您希望乘數為 1000 而不是 1024,請使用“KB”、“MB”或“GB”(注意:所有值也接受小寫)。

希望你喜歡這個教程! 請留下您的意見

另請閱讀:

  • 在 Linux 中傳輸文件的 Rsync 示例
  • Dcp – 在 Linux 計算機之間安全傳輸文件的工具