title: Shell script to traverse directories
body: <p>I'm working on a project that requires batch processing of a large number of image files.  To make things easier, I've written a script that will create <code>n</code> directories and move <code>m</code> files to them based on user input.</p>
  
  <p>My issue is to now understand directory traversal via shell script.</p>
  
  <p>I've added this snippet at the end of the sort script described above</p>
  
  <pre><code>dirlist=$(find $1 -mindepth 1 -maxdepth 1 -type d)
  
  for dir in $dirlist
  do
    cd $dir
    echo $dir
    ls
  done
  </code></pre>
  
  <p>When I ran it inside a Pano2 folder, whcih contains two inner folders, I always got an error</p>
  
  <pre><code>./dirTravel: line 9: cd: Pano2/05-15-2012-2: No such file or directory
  </code></pre>
  
  <p>However, after that, I get the file listing from specified directory.</p>
  
  <p>What is the reason behind the warning?  If I add <code>cd ../</code> after <code>ls</code> I get the listing of the folders inside Pano2/, but not the files themselves.</p>