18

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
  • 861
  • 3
  • 11
  • 18
  • 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 08 '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 08 '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 08 '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 08 '16 at 14:36
  • `--exclude='/root/branch*'` should be ok – Kondybas Sep 08 '16 at 14:39
  • @M.Glatki I did see that entry, and agree that my question is close. But, the answer there did not include wildcard pattern matching on the directory names. – Roger Creasy Sep 08 '16 at 14:43
  • @RogerCreasy You are right, I withdraw my flag. – M. Glatki Sep 08 '16 at 14:49
  • 2
    Possible duplicate of [rsync - exclude all directories except a few](http://serverfault.com/questions/770728/rsync-exclude-all-directories-except-a-few) – mdpc Sep 09 '16 at 01:09
  • I tried everything suggested here - still no resolution. Any other suggestions? – Roger Creasy Sep 20 '16 at 19:17
  • Related: https://unix.stackexchange.com/questions/83394/rsync-exclude-directory-not-working – Jesse Nickles Mar 24 '23 at 21:51

2 Answers2

9

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.

AaronDanielson
  • 231
  • 2
  • 3
5

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
  • 1,964
  • 1
  • 17
  • 33
  • 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