title: bash selectively copy files/folders to another location
body: <p>I have a loop in my script, which iterates over an array - <code>myArray</code> and I need to do some copying of files/directories on each iteration. The array can be like this - </p>
  
  <pre><code>myArray=('ajax' 'style/prod_styles' `path/to/some_file.php` 'templates' 'uploadify')
  </code></pre>
  
  <p>Taking this array, for elements without a <code>/</code>, I need to copy the entire folders and their contents - e.g. <code>ajax</code>, <code>templates</code>, <code>uploadify</code>. But for those having slashes - like <code>style/prod_styles</code> (Note that there can be multiple slashes), I need to copy only the last element (e.g. for <code>/path/to/some/folder</code> I need to copy only <code>folder</code> and its contents) and in case the parent folders are not existing in the destination (e.g. <code>path</code>, <code>to</code>, <code>some</code> are the parent folders), I need to just create those folders and then copy the last element (<code>folder</code>).</p>
  
  <p>Earlier I guessed it would be easy to do an explode (like PHP's <code>explode()</code>) with <code>/</code> inside the loop and then recursively start from the parent (<code>path</code> directory as per the above example) check if its child directory exists, if not create it, till we are done with the parent of the file/directory to be copied, then do the final copy.</p>
  
  <p>If, however there is something simpler possible in bash to do this, please let me know.</p>
  
  <p>Thanks,<br>
  Sandeepan</p>