rsync 包含特定文件并排除目录

rsync include specific files and exclude directory

我的目录结构如下:

base
├── config.yml
├── build
│   └── output.yml
│   └── <multiple level sub-directories each having many files including *.c and *.h files>
├── <source directories - having many sub-directories with various files including *.c and *.h files>
│   ├── <xyz>
|   │   ├── <x1>
|   │   .
│   |   └── <xy>
│   .
│   .
│   └── <abc>
├── <more directories, each having multiple files including *.c and *.h files>

我需要将此目录同步到远程,但我只需要*.c 和*.h 文件。还需要排除完整的 'build' 目录。我在 运行 以下命令:

rsync -avm --include '*/' --include='*.c' --include='*.h' --exclude='*' base "$target_loc"

这是同步所有需要的 *.c 和 *.h 文件,但它也同步来自构建及其子目录的 *.c 和 *.h 文件

我试过了 rsync -avm --include '*/' --include='*.c' --include='*.h' --exclude='build' --exclude='*' base "$target_loc"。它仍然同步来自构建及其子目录的文件。

我该如何解决这个问题?

您需要在 --include '*/' 之前放置 --exclude='build'。这两条规则都适用于“build”目录,先给出的优先,因此要让 --exclude 规则覆盖 --include 规则,您需要先列出它。

来自 rsync man page,在 FILTER RULES 部分(我强调):

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.