title: ubuntu/linux bash: traverse directory and subdirectories to work with files
body: <p>let me start off with what I need; the program is given a directory, it will then examine all the files in the directory (works) and do stuff to the files (waiting till it can find all the files for this part). then it will look for subdirectories and re-run its self for each subdirectory.</p>
  
  <p>the directory I'm testing with looks like this:</p>
  
  <pre><code>desktop/test_files/ (starting directory)
  desktop/test_files/folder 1/
  desktop/test_files/folder 1&gt;folder 2/
  desktop/test_files/folder 1&gt;folder 2/&lt;files, 20 or so&gt;
  desktop/test_files/folder 3/
  desktop/test_files/folder 3/&lt;more files, 20 or so&gt;
  </code></pre>
  
  <p>folders and files do contain spaces in the names</p>
  
  <p>the output is:</p>
  
  <pre><code>$ ./x007_shorter.sh Desktop/test_files/
  
  Desktop/test_files/"folder 1"/
  Desktop/test_files/folder 1/"folder 2"/
  ls: cannot access */: No such file or directory
  Desktop/test_files/folder 1/folder 2/"folder 3"/
  ./x007_shorter.sh: line 4: cd: ./folder 3/: No such file or directory
  ls: cannot access */: No such file or directory
  </code></pre>
  
  <p>here is the program:</p>
  
  <pre><code>#!/bin/bash
  function findir {
      newDir=$1
      eval cd $newDir
      ARRAY=( $(ls -d */) )
      declare -a diry
      count=0
      a=0
      while [ $a -lt ${#ARRAY[@]} ]; do
          diry[$count]="${ARRAY[$a]}"
          noSpace=true
          while [ true ]; do
              if [[ ${diry[$count]} == */* ]] ; then
                  if [ $noSpace = false ]; then
                  diry[$count]="${diry[$count]:0:((${#diry[$count]}-1))}\"/"
                  fi
                  break
                  noSpace=true
              fi
              let "a=$a+1"
              if [ $noSpace = false ]; then
                  diry[$count]="${diry[$count]} ${ARRAY[$a]}"
              else
                  diry[$count]="\"${diry[$count]} ${ARRAY[$a]}"
              fi
              noSpace=false
          done
          let "count=$count+1"
          let "a=$a+1"
      done
      for a in `seq 1 ${#diry[@]}`; do
          eval cd .$newDir
  #        list "${diry[($a-1)]}"
          where=`pwd`
  #        eval cd $newDir
          #findir "${diry[($a-1)]}"
          #findir "$where${diry[($a-1)]:1}"
          #Right option won,  echo "${diry[($a-1)]} Vs $where/${diry[($a-1)]}"
          echo "$where/${diry[($a-1)]}"
          findir "./${diry[($a-1)]}"
      done
  }
  function list {
      input_file_directory=$1
      eval cd $input_file_directory
      ARRAY=( $(find . -maxdepth 1 -type f -print) )
      declare -a files
      count=0
      a=0
      while [ $a -lt ${#ARRAY[@]} ]; do
          files[$count]="${ARRAY[$a]}"
          while [ true ]; do
              if [[ ${ARRAY[(($a+1))]} == ./* ]] ; then
                  break
              fi
              if [[ "${ARRAY[(($a+1))]}" == "" ]] ; then
                  break
              fi
              let "a=$a+1"
              files[$count]="${files[$count]} ${ARRAY[$a]}"
          done
          let "count=$count+1"
          let "a=$a+1"
      done
      where=`pwd`
      for a in `seq 1 ${#files[@]}`; do
          echo "$where${files[($a-1)]:1}"
          #going to work on each file, just echoing file till lists all files
      done
  }
  
  clear
  dar=""
  if [[ $1 = "" ]]; then
      read -p "Please enter a directory for me to scan" newdir
      dar=$newdir
      list $newdir
      findir $newdir
  else
      dar=$1
      list $1
      findir $1
  fi
  </code></pre>