Super User is a question and answer site for computer enthusiasts and power users. 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 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

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

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

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.