23

In an rsync I am trying to exclude sub-directories that match a pattern. But, I cannot get it to work. I have followed several examples found here and on Google. But, I do not get the correct results. Here is the option bit of my command:

-avh --exclude 'branch*' --stats --delete --link-dest=$LNK

My source directory structure is

/root
    /branch1
    /branch2
    /branch3
    /other
    /stillAnother
    /etc

This is part of a backup script. $LNK is a link to the previous day's rsync destination.

I do not want /root/branch1, /root/branch2, /root/branch3. or their contents to be synced. But, they are.

Here are the exclude bits I have already tried:

--exclude=branch*
--exclude='branch*'
--exclude '/branch*'
--exclude /branch*

Thanks for any help/advice.

EDIT - to address the "possible duplicate" flag

This question is regarding a known list of directories. I need to exclude any directories that follow a pattern, even if those directories do not yet exist. i.e. from my example, other directories named /branch* may be added. I need to make my script future-proof, and avoid editing the script when a directory that matches the pattern is added, as those directories may be temporary.

Roger Creasy
  • 951
  • 3
  • 11
  • 19

2 Answers2

13

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.

6

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
M. Glatki
  • 2,084
  • I changed my options in the script to -avh --exclude 'branch*' --stats --delete --delete-excluded --link-dest=$LNK Last night's backup still backed up the /branch* directories. – Roger Creasy Sep 09 '16 at 13:59