Include and Exclude

Benjamin R. Haskell rsync at
Sat Dec 26 21:57:37 MST 2009

On Thu, 24 Dec 2009, LuKreme wrote:

> I thought this command would do what I wanted, but instead it doesn't transfer any files:
> rsync -avh --stats --password-file=/var/rsync.passwd --include=Maildir/ 
> --exclude=* mail::root/usr/home /backup/usr/
> I want to backup ONLY the /usr/home/*/Maildir directories. I don't want 
> any other files from any other directories to be transferred.
> I can see in the man page where this is a problem:
>        when using the --recursive (-r) option (which is implied by -a), every 
>        subcomponent of every path is visited  from  the top down, so include/
>        exclude patterns get applied recursively to each subcomponent's full 
>        name (e.g. to include "/foo/bar/baz" the subcomponents "/foo" and 
>        "/foo/bar" must not be excluded).
> It then gives an example of including the parent directories.
> Trouble is, I do not want any files in /usr/home/fred/ to be synced at 
> all, only the files (and directories) in /usr/home/fred/Maildir/

The following comes close:

rsync -avh '--include=/*/' '--include=/*/Maildir/***' '--exclude=*' home/ 

(Might need to preface the first /* components in the patterns with 
'root/usr/home' or '/root/usr/home' for your case.)

I say "comes close" because it gets an extra empty directory for every 
user directory, even if it doesn't contain a Maildir.

> Also, I don't necessarily know all the pathnames in /usr/home/; I don't 
> want to have to build a manual include list every time I want to sync 
> these folders.

Not sure if "I don't want to have to build a manual include list" 
precludes using a script to generate the list, but for solving this same 
problem a while back (backing up user maildirs), I used something like the 

getent passwd | cut -f6 -d: | sort | uniq \
| perl -lnwe '$_.="/Maildir"; print if -e' \
| xargs -iZ rsync -avzR /./Z/ backup/Z/

I don't remember if that's the exact rsync command, but the point is that 
it wasn't too hard to generate the list of Maildir directories, assuming 
it's possible to list the homedirs.


More information about the rsync mailing list