Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to work out the rsync filter syntax to perform complex include/excludes, and trying to achieve the following:

Include /
Exclude /home
Include /home/user1/*
Include /home/user2/subdir/*

I've tried many variations on the filter syntax, and despite reading the man page many time, I cannot get this sort of effect. Rsync filters seem to be very powerful, and I find it hard to believe they couldn't handle a common scenario such as this.

share|improve this question

You need to include all of the parent directories down to the desired directory before using the exclude rule.

For instance, I use the following in a backup script:

rsync -av \ 
--filter='+ /var/' \
--filter='+ /var/backups/' \
--filter='- /var/*' \
/ \

So in your case you would need something like the following:

rsync -av \ 
--filter='+ /home/' \
--filter='+ /home/user1/' \
--filter='+ /home/user2/' \
--filter='+ /home/user2/subdir/' \
--filter='- /home/user2/*' \
--filter='- /home/*' \
/ \
share|improve this answer
Excellent. Took me some time. – yclian Feb 12 '11 at 16:00

On the command line:

rsync --dry-run --verbose --recursive --include=/home/user1 --exclude=/home/* / DEST

Remove --dry-run to make it functional, replace "DEST" with your destination and add user and host to the source "/" if needed.

From a rule file:

rsync --dry-run --verbose --recursive --filter='merge /etc/rsync/somerules.rules' / DEST

where the contents of /etc/rsync/somerules.rules might be:

+/ /home/user1
-/ /home/*

You should test these and you may need to make some adjustments, but this should get you started.

share|improve this answer
OK, that works. I have a more complex case which I've added to the question. The additional include doesn't work with the subdir scenario. – brianmathis Jun 11 '10 at 16:45

But what if for all usernames you need a variable and at the end of the file tree.

For example:


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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