如何 rsync 一组子目录?

Mat*_*ell 11 rsync

我有一个如下所示的源目录(在 src 和 bin 目录中有许多子目录):

  • 顶部目录
    • 项目1
      • 来源/
      • 仓/
      • 脚本/
      • 一些文件.txt
    • 项目2
      • 来源/
      • 仓/
      • 目标
        • 仓/
      • 文件.xml

我只想将项目目录下的 bin 目录(即,忽略 top_dir/proj2/target/bin/)及其内容 rsync 到目标目录,使其看起来像这样:

  • 顶部目录
    • 项目1
      • 垃圾桶
    • 项目2
      • 垃圾桶

我可以通过在 include-from 文件中明确列出每个 bin 目录(在我的实际场景中大约有 15 个)来使其工作,但我认为必须有一种方法可以说明我想同步所有 top_dir/ */bin 目录。

mpy*_*mpy 19

可能性 1

不使用 rsync 的过滤规则,而是使用 shell 扩展*你可以:

一种。要bintop_dir层次结构中包含所有目录:

rsync -av -R top_dir/**/bin/ destination_path
Run Code Online (Sandbox Code Playgroud)

-R是一个 rsync 参数,代表相对,意味着保留目录结构。

**是一个外壳为命名的目录文件名匹配模式和递归搜索bintop_dir。因此,在上面的示例中,命令行将扩展为:

rsync -av -R top_dir/proj1/bin/ top_dir/proj2/bin/ destination_path
Run Code Online (Sandbox Code Playgroud)

湾 如果您只想包含bin项目目录下一级的目录,只需使用一个*(也适用于“古代”shell!):

rsync -av -R top_dir/*/bin/ destination_path
Run Code Online (Sandbox Code Playgroud)

*您需要最新的bash版本 (>4) 并通过shopts -s globstar. 使用zsh递归全局匹配是默认的。

可能性2

仅使用您需要的 rsync 过滤规则:

一种。要bintop_dir层次结构中包含所有目录:

rsync -av -m --include='**/' --include='**/bin/**' --exclude='*' top_dir/ destination_path
Run Code Online (Sandbox Code Playgroud)

第一个包含规则可能不明显+,但保证搜索所有目录(注意尾随/),第二个最后添加所有内容bin目录中的排除规则排除其他所有内容。-m确保不复制空目录。

湾 仅比项目目录低一级:

rsync -av -m  --include='/*/' --include='/*/bin/***' --exclude='*' top_dir/ destination_path
Run Code Online (Sandbox Code Playgroud)

使用***rsync >= 2.6.7 的语法。/模式中的前导代表top_dir,否则模式将与文件路径的末尾相匹配。


+搜索/some/path/this-file-will-not-be-foundman rsync