In an rsync, how do I exclude all directories that match a pattern?
Solution 1:
You exclude rule is correct. However, rsync will not delete excluded files on the destination without the extra parameter --delete-excluded
:
--delete-excluded also delete excluded files from dest dirs
Example:
# tree test
test
|-- 123
|-- branch1
|-- branch2
|-- branch3
`-- other
# tree test2
test2
|-- 123
|-- branch1
|-- branch2
|-- branch3
`-- other
# rsync -avh test/ test2 --delete --exclude='branch1' --delete-excluded
sending incremental file list
deleting branch1/
sent 140 bytes received 27 bytes 334.00 bytes/sec
total size is 0 speedup is 0.00
# tree test2
test2
|-- 123
|-- branch2
|-- branch3
`-- other
3 directories, 1 file
Solution 2:
rsync version 3.1.3 (possibly earlier, haven't checked) correctly excludes subdirectories using this syntax (obviously replacing exclude_dirname
with the pattern you want to exclude):
rsync [other opts...] --exclude='*/exclude_dirname/' /src/ /dst/
This also works with wildcards. Original question uses 'branch*'
, so this works:
rsync [other opts...] --exclude='*/branch*/' /src/ /dst/
Hope this helps.