title: Bash: Copy named files recursively, preserving folder structure
body: <p>I was hoping:</p>
  
  <pre><code>cp -R src/prog.js images/icon.jpg /tmp/package
  </code></pre>
  
  <p>would yield a symmetrical structure in the destination dir:</p>
  
  <pre><code>/tmp
  |
  +-- package
      |
      +-- src
      |   |
      |   +-- prog.js
      |
      +-- images
          |
          +-- icon.jpg
  </code></pre>
  
  <p>but instead, both of the files are copied into /tmp/package. A flat copy.  (This is on OSX).</p>
  
  <p>Is there a simple bash function I can use to copy all files, including files specified by wildcard (e.g. src/*.js) into their rightful place within the destination directory. A bit like "for each file, run <code>mkdir -p $(dirname "$file"); cp "$file" $(dirname "$file")</code>", but perhaps a single command.</p>
  
  <p><a href="http://www.linuxquestions.org/questions/linux-newbie-8/how-to-do-recursive-file-copy-of-directory-for-specific-files-199134/" rel="noreferrer">This is a relevant thread, which suggests it's not possible.</a> The author's solution isn't so useful to me though, because I would like to simply provide a list of files, wildcard or not, and have all of them copied to the destination dir. IIRC MS-DOS xcopy does this, but there seems to be no equivalent for cp.</p>