title: Using for loop to move files from subdirectories to parent directories
body: <p>I just made the switch to linux and I am trying to write my first bash script. I have a folder that contains numerous folders, all with subfolders containing files. Something like:</p>
  
  <ul>
  <li><p>MainFolder</p>
  
  <ul>
  <li>Folder1
  <ul>
  <li>Sub1 (Contains many files)</li>
  <li>Sub2 (Contains many files)</li>
  </ul></li>
  <li>Folder2
  <ul>
  <li>Sub1 (Contains many files)</li>
  <li>Sub2 (Contains many files)</li>
  </ul></li>
  </ul>
  
  <p>.
  .
  .</p></li>
  </ul>
  
  <p>I want to move all the files contained in the sub-folders to the their parent folders. My first instinct is to try and write a for-loop. I was able to do one folder at a time with the command:</p>
  
  <pre><code>mv MainFolder/Folder1/*/* MainFolder/Folder1/
  </code></pre>
  
  <p>But I was hoping to write a bash script to loop over all the folders in the main directory. Here is what I have so far:</p>
  
  <pre><code>#!/bin/bash
  
  dir1="/pathto/MainFolder"
  
  subs= ls $dir1
  
  for i in $subs; do
    mv "$dir1/$i/*/*" "$dir1/$i/" 
  done
  </code></pre>
  
  <p>This, obviously, does not work, but I do not understand where I am going wrong. </p>
  
  <p>I also tried: </p>
  
  <pre><code>mv MainFolder/*/*/* MainFolder/*/
  </code></pre>
  
  <p>with pretty disastrous results. Once I get the file move working properly, I would also like to delete the old sub folders within the loop.</p>