title: How to properly match directories with rsync
body: <p>I need to rsync some directories from one machine to another, but only part of the tree. I think the easiest way to explain it would be to run, on the server, <code>tree -f</code> (which prints the full path of each file) and then <code>grep</code>'ing <em>mon</em> in the full path and <code>rsynch</code>'ing only the matches of <code>grep</code>. Actually I would be happy to do it that way, but I don't have shell access to the server, only <code>rsync</code>. Two step solution based on <code>grep</code>ing output of <code>rsync --list-only</code> is totally acceptable.</p>
  
  <p>It's actually simpler, because I also know that this <em>mon</em> appears <strong>only</strong> as a suffix to a directory (the hierarchy is pretty rigid), so I tried doing</p>
  
  <p><code>rsync --dry-run -vrlpt --include='*/' --include='**/*mon/' --exclude='*' --prune-empty-dirs</code></p>
  
  <p>but that did not match anything. Ditto for <code>--include='*mon/'</code> instead of the one with the double asterisk. Avoiding the <code>--prune-empty-dirs</code> matched all the directories but no files, which I think is my problem. So I tried to add an include pattern for some file extensions right before the exclude pattern (which I would rather not do because I don't want to track them all and they might change), but that simply matched all those files, including the ones not in the "mon" trees.</p>
  
  <p>And yes, I did see <a href="https://stackoverflow.com/questions/28439393/how-to-rsync-files-with-matching-pattern-in-path-by-keeping-directory-structure">this</a> and <a href="https://unix.stackexchange.com/questions/2161/rsync-filter-copying-one-pattern-only/2503#2503">this</a> and <a href="https://unix.stackexchange.com/questions/63406/rsync-all-directories-that-start-with-a-specific-digit">this</a> and countless others, which helped me making some progress (i.e. what you can see above), but not solve the problem...</p>