exclude a pattern but only in the top level

Brian K. White brian at aljex.com
Wed May 15 03:06:24 MDT 2013

On 5/15/2013 4:13 AM, Brian K. White wrote:
> Consider the following directory structure
> /foo/aaa/*/*
> /foo/bbb/*/*
> /foo/ccc/*/*
> I want to sync all of /foo,
> but exclude /foo/aaa
> but not exclude any other occurances of "aaa" or "foo/aaa" (be they
> files or dirs) that might occur within the other dirs /foo/bbb/*
> /foo/ccc/* etc
> I don't want to exclude /foo/bbb/aaa or /foo/ccc/111/aaa or
> /foo/ccc/111/foo/aaa etc...
> Destination is running rsync daemon and has a module named root that
> points to "/" such that normally, for the full /foo with no exclude,
> it's very simple, my rsync command would just be:
> rsync -avz /foo ${DEST}::root
> That works fine without the exclude.
> So, to that I want to add a filter file. (Well, I assume that's what I
> want)
> rsync -avz -f ". filter" /foo ${DEST}::root
> If I construct a filter file like this:
> - aaa
> + *
> or like this
> - aaa/
> + *
> or like this
> - foo/aaa/
> + *
> rsync hides the top level /foo/aaa but it also hides any other
> occurrences of the exclude pattern that occur anywhere within the job
> instead of just the top level one.
> How can I get it to exclude just the top-level directory "/foo/aaa" ?

Never mind, I think I got it. I saw the special meaning of a leading 
slash in an include/exclude pattern in the man page. So:

rsync -aznvvv --exclude '/pix/trinity/' /pix ${DEST}::root
[sender] hiding directory pix/trinity because of pattern /pix/trinity/
[sender] make_file(pix/0/trinity,*,2)
[sender] make_file(pix/0/pix/trinity,*,2)
[sender] make_file(pix/0/pix/trinity/foo,*,2)
[sender] make_file(pix/0/trinity/trinity,*,2)

So it excluded /pix/trinity/ as desired, yet included other paths that 
had "/pix/trinity/" in them such as /pix/0/pix/trinity/foo"

I only need the single pattern at the moment so --exclude is simpler 
than a filter file, but just for the record I verified a filter file 
like this worked the same too:
- /pix/trinity/

Also please disregard the "+ *" in my previous examples. I know they are 
superfluous. They were in there only because I was starting with a copy 
of a different script that originally had "- *" at the bottom to include 
some patterns and exclude everything else. At the moment I need the 
opposite of that so when I reversed all the + & -, I ended up with a "+ 
*" at the bottom.


More information about the rsync mailing list