rsync 與絕對路徑並排除子路徑

rsync 與絕對路徑並排除子路徑

假設我的路徑上有以下結構origin_path

origin_path/X=1/A/...
origin_path/X=1/B/...
origin_path/X=1/C/...
...
origin_path/X=2/A/...
origin_path/X=2/B/...
origin_path/X=2/C/...
...
...

我想在名為 的不同路徑複製上面的結構 destination_path,但是僅有的包括目錄對應的路徑多於。

換句話說,我想獲得以下資訊destination_path

destination_path/X=1/B/...
destination_path/X=2/B/...
destination_path/X=3/B/...
...

為了澄清,目標路徑中的某些結構可能已經存在(例如資料夾destination_path/X=*)。

據我了解,多虧了@Gilles 的評論,rsync過濾器可能非常適合這個。但是,我以前從未使用過它們,我很難推斷中提供的範例Rsync 篩選器:僅複製一種模式就我的情況而言。

我應該按什麼順序包含或排除我的案例中的內容?我該如何告訴 rsync 使用origin_pathanddestination_path作為全域路徑? (而不是讓它複製具有相對路徑的內容)

答案1

以下應該要做你想做的事:

rsync --recursive --prune-empty-dirs --include '*/' --include '*/B/**' --exclude '**' origin_path/ destination_path/

第一條規則包括所有目錄(否則 rsync 將不會下降到頂級目錄)。第二條規則包括「B」子目錄中的所有內容。第三條規則排除了其他一切。這個--prune-empty-dirs選項忽略空目錄(因為我們使用第一條規則包含所有目錄)。

相關內容