title: How do I move files into folders with similar names in Unix?
body: <p>I'm sorry if this question has been asked before, I just didn't know how to word it as a search query.</p>
  
  <p>I have a set of folders that look like this:</p>
  
  <pre><code>Brain - Amygdala/                 Brain - Spinal cord (cervical c-1)/  Skin - Sun Exposed (Lower leg)/
  Brain - Caudate (basal ganglia)/  Lung/                                Whole Blood/
  </code></pre>
  
  <p>I also have a set of files that look like this:</p>
  
  <pre><code>Brain_Amygdala.v7.covariates_output.txt                  Skin_Not_Sun_Exposed_Suprapubic.v7.covariates_output.txt
  Brain_Caudate_basal_ganglia.v7.covariates_output.txt     Skin_Sun_Exposed_Lower_leg.v7.covariates_output.txt
  Brain_Spinal_cord_cervical_c-1.v7.covariates_output.txt  Whole_Blood.v7.covariates_output.txt
  </code></pre>
  
  <p>As you can see, the files do not perfectly match up with the directories in their names. For example, <code>Brain_Amygdala.v7.covariates_output.txt</code> is not totally identical to <code>Brain - Amygdala/</code>. Even if we were to excise the tissue name from the covariates file, <code>Brain_Amygdala</code> is formatted differently from its corresponding folder. </p>
  
  <p>Same with <code>Whole Blood/</code>. It is different from <code>Whole_Blood.v7.covariates_output.txt</code>, even if you were to isolate the tissue name from the covariates file <code>Whole_Blood</code>. </p>
  
  <p>What I want to do, however, is to move each of these tissue files to their corresponding folder. If you notice, the covariate files are named after the tissue leading up to the first dot <code>.</code> in the file name. They are separated by underscores <code>_</code>. <strong>How I was thinking about approaching this was</strong> to break up the first few words leading up to the first <code>.</code> of the file name so that I can easily move it to its corresponding file.</p>
  
  <p>e.g.</p>
  
  <p><code>Brain_Amygdala.v7.covariates_output.txt</code> -> <code>Brain*Amygdala</code> [mv]-> <code>Brain*Amygdala/</code></p>
  
  <p>a) I'm not sure how to isolate the first words of a file name leading up to the first <code>.</code> in a filename</p>
  
  <p>b) if I were to do that, I don't know how to insert a wildcard in between each word and match that to the corresponding folder. </p>
  
  <p><strong><em>However,</em></strong> I am completely open to other ways of doing something like this.</p>