title: Bash script loop through subdirectories and write to file
body: <p>I have no idea I have spent a lot of hours dealing with this problem. I need to write script. Script should loop recursively through subdirectories in current directory. It should check files count in each directory. If file count is greater than 10 it should write all names of these file in file named "BigList" otherwise it should write in file "ShortList". This should look like </p>
  
  <pre><code>---&lt;directory name&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  ....
  ---&lt;directory name&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  &lt;filename&gt;
  ....
  </code></pre>
  
  <p>My script only works if subdirecotries don't include subdirectories in turn. 
  I am confused about this. Because it doesn't work as I expect. It will take less than 5 minutes to write this on any programming language for my. 
  Please help to solve this problem , because I have no idea how to do this.
  Here is my script</p>
  
  <pre><code>#!/bin/bash
  parent_dir=""
  if [ -d "$1" ]; then
      path=$1;
  else
      path=$(pwd)
  fi
  parent_dir=$path
  loop_folder_recurse() { 
      local files_list=""      
      local cnt=0
      for i in "$1"/*;do
          if [ -d "$i" ];then
              echo "dir: $i"
              parent_dir=$i               
              echo before recursion
              loop_folder_recurse "$i"
              echo after recursion
              if [ $cnt -ge 10 ]; then
                  echo -e "---"$parent_dir &gt;&gt; BigList
                  echo -e $file_list &gt;&gt; BigList
              else
                  echo -e "---"$parent_dir &gt;&gt; ShortList
                  echo -e $file_list &gt;&gt; ShortList
              fi
          elif [ -f "$i" ]; then
              echo file $i
              if [ $cur_fol != $main_pwd ]; then
                  file_list+=$i'\n'
                  cnt=$((cnt + 1))
              fi
          fi
      done
  }
  echo "Base path: $path"
  loop_folder_recurse $path
  </code></pre>