title: How to iterate between directories in shell script
body: <p>I want to convert images from <code>.img</code> to <code>.nii.gz</code> using the function <code>fslchfiletype</code>.</p>
  <p>These images are stored in <code>Controls</code> and <code>Patients</code> folders, each one of this folders have several folders belonging to each one of the individuals, like <code>C01, C02,</code> , etc. Specifically, each one of the individuals has the <code>.img</code> files inside a folder called <code>rs_roi</code>, which is inside another folder called <code>ROIS2</code>. This is what I have:</p>
  <pre><code>DIR=&quot;/media/Roy&quot;; cd &quot;$DIR/Analysis&quot;
  for group in Controls Patients; do
      for case in $group; do
          mkdir $DIR/Analysis/$group/$case/Cortical_masks 
                  
          for file in $DIR/Analysis/$group/$case/ROIS2/rs_roi/.img; do
          fslchfiletype NIFTI_GZ &quot;$file&quot;
          done;
      done;
  done;       
  </code></pre>
  <p>Notice how I also want to create a <code>folder</code> called <code>Cortiical_maks</code> inside each and one of the individuals.</p>
  <p>This gives me the next error:</p>
  <pre><code>mkdir: cannot create directory ´media/Roy/Analysis/Controls/Controls/Cortical_masks´: No such file or directory.
  Cannot open volume media/Roy/Analysis/Controls/Controls/ROIS2/rs_roi/ for reading!
  mkdir: cannot create directory ´media/Roy/Analysis/Patients/Patients/Cortical_masks´: No such file or directory.
  Cannot open volume media/Roy/Analysis/Patients/Patients/ROIS2/rs_roi/ for reading!
  </code></pre>
  <p>It´s iterating two times the <code>Controls Patients</code> folder: <code>Control/Control</code>. Maybe the problem is here <code>for case in $group; do</code>? Thx</p>