title: find + cp sparse directory tree
body: <p>I have a directory tree which, among other files, has files which match certain patterns. For the sake of the discussion, let's assume these are files matching <code>*.foo</code>, or <code>*.bar</code>, or <code>baz*</code>. I want to backup inside my zsh-script <strong>only files</strong> matching these pattern to a new directory.</p>
  <p>The seemingly obvious solution,</p>
  <pre><code>find fromdir \( -name '*.{foo,bar}' -o -name 'baz*' \) -exec cp {} todir \;
  </code></pre>
  <p>does not work, because the destination directory for, i.e., <code>fromdir/x/y/a.foo</code> does not exist.</p>
  <p>I was thinking of using <code>rsync</code>, but I know only how to <strong>exclude</strong> certain files from being copied, not how to <strong>restrict</strong> copying.</p>
  <p>I can solve the problem by writing a small auxiliary script, <code>mdcp1file</code>, like this:</p>
  <pre><code>#!/bin/zsh
  set -u
  mkdir -p $2/$1:h # Create destination directory if needed
  cp $1 $2
  </code></pre>
  <p>and use it in my <code>find</code> command instead of <code>cp</code>. I wonder whether there is an easier way to solve this problem, either by beefing up the <code>-exec</code> of my <code>find</code>, or by using <code>rsync</code> in a clever way.</p>