I'm using rsync to recursively sync a remote folder tree that looks something like this:


I don't know what the folder tree will look like and it will change over time. So what I want to be able to do is recursively rsync the above but exclude the folder "cache" and any sub folders it contains:


Any suggestions?


1 Answer 1


You want the --exclude flag. For example, a local rsync:

rsync -a --exclude cache/ src_folder/ target_folder/

It really is that simple -- that exclude rule will match a directory named cache anywhere in your tree. The / at the end forces it to match directories, so it won't exclude a file named cache.

You can match on paths as well. Given the following tree:


--exclude a1/cache/ will match a1/cache and sub/a1/cache but not b1/cache. And --exclude /a1/cache/ will match only a1/cache.

There's a lot more that can be done with rsync filtering. Look for --exclude, --include, --filter, and especially the "FILTER RULES" and "PATTERN MATCHING RULES" sections on the rsync man page:


  • 1
    Doh!!. Thanks it is the simple answers that I like best :-)
    – TheEdge
    Commented Jan 16, 2011 at 0:38
  • 2
    What about not anywhere in the tree? Like, only exclude a/cache. UPD See this answer
    – x-yuri
    Commented Nov 12, 2015 at 15:50
  • 3
    Also --exclude /cache/ (with the leading /) will only exclude a/cache directory, not any directory named cache. Commented Dec 19, 2015 at 13:07
  • Why a/ b/ in the above command line, what does it mean?
    – Danijel
    Commented Dec 24, 2015 at 9:13
  • 1
    @Danijel: a/ is the directory we're syncing (or copying) from; b/ is the directory we're syncing into.
    – Jander
    Commented Dec 25, 2015 at 4:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .