'rsync include specific files and exclude directory

I have a directory structure as below:

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>

I need to sync this directory to remote, but I only need *.c and *.h files. Also complete 'build' directory needs to be excluded. I am running below command:

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

This is syncing all *.c and *.h files which is desired but it also syncs *.c and *.h files from build and its sub directories

I tried rsync -avm --include '*/' --include='*.c' --include='*.h' --exclude='build' --exclude='*' base "$target_loc". It still syncs files from build and it's sub directories.

How can I fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source