如何从rsync中排除子目录?

The*_*dge 53 rsync

我正在使用rsync递归同步一个看起来像这样的远程文件夹树:

/folderA/a1/cache
/folderA/a1/cache/A1
/folderA/a1/cache/A2
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder/cache
/folderB/cache/
/folderB/b1/somefolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache
/folderB/b1/somefolder/yetanotherfolder/cache/B1
/folderB/b1/somefolder/yetanotherfolder/cache/B2
Run Code Online (Sandbox Code Playgroud)

我不知道文件夹树会是什么样子,它会随着时间的推移而改变。所以我想要做的是递归rsync上述但排除文件夹“缓存”和它包含的任何子文件夹:

/folderA/a1
/folderA/a1/somefolder
/folderA/a1/someotherfolder
/folderA/a2/somefolder
/folderB/
/folderB/b1/somefolder
/folderB/b1/somefolder/yetanotherfolder/
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Jan*_*der 78

你想要--exclude国旗。例如,本地 rsync:

rsync -a --exclude cache/ src_folder/ target_folder/
Run Code Online (Sandbox Code Playgroud)

这真的很简单——排除规则将匹配树中任何位置名为“缓存”的目录。

有关更多信息,请在 rsync 手册页上查找“--exclude”和“过滤规则”部分:

http://www.samba.org/ftp/rsync/rsync.html

  • 不在树中的任何地方怎么办?比如,只排除`a/cache`。**UPD** 见[这个答案](http://unix.stackexchange.com/a/83403/29867) (2认同)
  • 此外`--exclude /cache/`(带前导/)只会排除`a/cache`目录,而不是任何名为`cache`的目录。 (2认同)