title: Double quotes vs asterisk filename expansion in Bash
body: <p>In the directories <code>~/temp/a/foo/</code> and <code>~/temp/b/foo foo/</code> I have some files named <code>bar1</code>, <code>bar2</code>, <code>bar bar1</code>, <code>bar bar2</code>, etc.</p>
  
  <p>I am trying to write a line of Bash that copies all these files in a directory containing "foo" as last part of the name to the folder above the respective "foo" folder.</p>
  
  <p>As long as there are no spaces in the file names, this is an easy task, but the two following commands fail when dealing with the <code>foo foo</code> directory:</p>
  
  <pre><code>for dir in `find . -type d -name '*foo'` ; do cp $dir/* "$(echo $dir|sed 's_foo__g')" ; done
  </code></pre>
  
  <p>(The <code>cp</code> command fails to see the last <code>foo</code> of <code>"foo foo"</code> as part of the same directory name.)</p>
  
  <pre><code>for dir in `find . -type d -name '*foo'` ; do cp "$dir/*" "$(echo $dir|sed 's_foo__g')" ; done
  </code></pre>
  
  <p>(<code>"$dir/*"</code> is not expanded.)</p>
  
  <p>Attempts like replacing <code>$dir/*</code> with <code>"$(echo $dir/*)"</code> have been even less successful. </p>
  
  <p>Is there an easy way to expand <code>$dir/*</code> so that <code>cp</code> understands?</p>