rsync檔案同步包含檔案/目錄(--exclude)和排除檔案/目錄(--include)用法

首頁 > 技術

rsync檔案同步包含檔案/目錄(--exclude)和排除檔案/目錄(--include)用法

來源:Eddy 釋出時間:2021-12-01 10:55

在講解用法之前,先看看官方文件有這麼一段話:

As the list of files/directories to transfer is built, rsync checks each name to be transferred against the list of include/exclude patterns in turn, and the first matching pattern is acted on: if it is an exclude pattern, then that file is skipped; if it is an include pattern then that filename is not skipped; if no matching pattern is found, then the filename is not skipped.

理解這段話是弄明白如何使用--include和--exclude的核心。rsync在同步檔案時,會按你寫的匹配引數順序去匹配檔名,第一個被匹配上的會對應生效。如果是exclude,那麼這個檔案會被排除;如果是include,這個檔案不會被排除;如果沒有匹配上,這個檔案也不會被排除。所以寫--include、--exclude引數時,順序很重要。

假設要同步的源目錄結構如下:

/root/s/

├── java

│   └── 1.txt

├── mysql

│   └── 1.txt

├── php

│   ├── 1.txt

│   ├── java

│   ├── java.txt

│   ├── php11.txt

│   └── zzz

│       └── jjj

│           └── hei.txt

└── php111.txt


現在我想同步 /root/s/ 目錄到 /root/d/ 目錄,但是我只想同步 php 子目錄的所有內容,那該怎麼寫呢?正確的寫法如下:

rsync -av --include="php" --include="php/**" --exclude="*" /root/s/ /root/d/


如果只想同步 php 子目錄下的檔案和子目錄,但不包括子目錄下的內容,應該這樣寫:

rsync -av --include="php" --include="php/*" --exclude="*" /root/s/ /root/d/


上一篇:男子早餐店吃... 下一篇:她打仗比花木...
猜你喜歡
熱門閱讀
同類推薦