Take the 2-minute tour ×

I want to exclude /bar/ from rsync, but only when in folder /foo/.

If I use --exclude '/foo/bar/' , it will only work when rsync is run on foos parent directory, but in our setup, I can't do that.

If I use --exclude '/bar/', it will exclude all /bar/ subdirectories, including /fee/bar/, for example.

What I want to do is only exclude /bar/ when the target directory is /foo/

Is this possible in rsync?

Or do I need to have some wrapper script that inserts the exclude rule it target is /foo/?

share|improve this question
add comment

2 Answers

From the rsync(1) man page, "INCLUDE/EXCLUDE PATTERN RULES":

   o      if the pattern starts with a / then it is anchored to a particu-
          lar spot in the hierarchy of  files,  otherwise  it  is  matched
          against the end of the pathname.  This is similar to a leading ^
          in regular expressions.  Thus "/foo" would match a name of "foo"
          at  either  the "root of the transfer" (for a global rule) or in
          the merge-file’s  directory  (for  a  per-directory  rule).
share|improve this answer
add comment

See the following snippet, foo/bad and bar/bad get created as dirs with some file in them, if you simply exclude them using "*foo/bad" then every 'bad' (file or dir) contained in foo/bad will be ignored.

e@lap:/tmp$ mkdir -p source/foo/bad source/bar/bad target
e@lap:/tmp$ touch source/foo/bad/a.txt source/bar/bad/a.txt
e@lap:/tmp$ rsync -vr --exclude "*/foo/bad" source target/
sending incremental file list

sent 153 bytes  received 47 bytes  400.00 bytes/sec
total size is 0  speedup is 0.00
share|improve this answer
foo bad? too bad –  djjeck Jan 4 '13 at 4:37
add comment

Your Answer


