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


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*

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.

  • your exclude parameter is ok. If rsync ignores branch* when copying, it also ignores it when deleting from dest, so you have to manually delete these dirs at destination one time. – Ipor Sircer Sep 8 '16 at 14:30
  • the destination is a new, empty directory, created by my bash script. I did delete the branch* directories from the link-dest directory, to no avail. – Roger Creasy Sep 8 '16 at 14:32
  • 1
    You have to use an explicit full pattern like *branch* or /root/branch* not the short branch* - short form is not found and thus is not excluded. – Kondybas Sep 8 '16 at 14:33
  • @Kondybas So, all is ok in my options except I need to prepend a '*' to my exclude branch? (--exclude '*branch*) – Roger Creasy Sep 8 '16 at 14:36
  • 2
    Possible duplicate of rsync - exclude all directories except a few – mdpc Sep 9 '16 at 1:09

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


#  tree test
|-- 123
|-- branch1
|-- branch2
|-- branch3
`-- other

#  tree 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
|-- 123
|-- branch2
|-- branch3
`-- other

3 directories, 1 file
| improve this answer | |
  • 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 9 '16 at 13:59

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.