exclude a pattern but only in the top level

Brian K. White brian at aljex.com
Wed May 15 05:41:36 MDT 2013


On 5/15/2013 5:17 AM, Paul Slootman wrote:
> On Wed 15 May 2013, 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
>
>> rsync -avz /foo ${DEST}::root
>
> Firstly, I always recommend that with directory transfers you add a
> trailing slash to the source, so your command becomes:
>
> rsync -avz /foo/ ${DEST}::root/foo/
>
> Then a filter would be:
>
> - /aaa/
> + /*
>
> Note the leading slash, as your aaa directory is now in the root of the
> source. The second line is not really needed in this scenario IMHO
>
> To be honest I'm wondering about your usage of -f ". filter",
> I always do --exclude-from=filter


 > rsync -avz /foo/ ${DEST}::root/foo/

This syntax does work in his case, and is easier to read, because it 
ends up using the exact same specification "/foo/" and "/foo/" for both 
source and dest, but the syntax I had was also correct. I no longer 
remember why I always do the way I posted, but I've been doing it that 
way for decades (if you count rcp before rsync).

It might be that the way I did it _always_ works as expected, where this 
way maybe doesn't work as expected in some cases, like maybe if the 
source is a file named /foo/aaa, so you use "/foo/aaa" the same in both 
the source and dest, but the dest machine happens to have a directory 
named /foo/aaa, then instead of having the dest directory converted to a 
file to match the source, the file from the source would be placed 
_within_ the directory on the dest, creating a file named /foo/aaa/aaa 
instead of /foo/aaa on dest. So, I'd rather, it's safer to do something 
the same way every time and know that it works the same way every time 
where possible. If you manually remove the trailing path component from 
the dest, then it always works the same way regardless if source is a 
file or dir and regardless if dest already has a file or dir of the same 
name. You follow the same exact rule every time and get the same exact 
result every time. No unpleasant surprises.

The -f ". filename" syntax is correct it's right in the manual and I've 
been using it for ages.

You're right the leading slash in the exclude pattern was the key in 
this case.

-- 
bkw



More information about the rsync mailing list