title: Recursive Function to Return Directory Depth of File Tree
body: <p>I'm trying to write a function that will traverse the file directory and give me the value of the deepest directory.  I've written the function and it seems like it is going to each directory, but my counter doesn't seem to work at all.  </p>
  
  <pre><code>dir_depth(){ 
  
  local olddir=$PWD
  local dir
  local counter=0
  cd "$1"
  
  
  for dir in *
  do
    if [ -d "$dir" ]
    then
      dir_depth "$1/$dir"
      echo "$dir"
      counter=$(( $counter + 1 ))
    fi 
  done
  cd "$olddir"
  }
  </code></pre>
  
  <p>What I want it to do is feed the function a directory, say /home, and it'll go down each subdirectory within and find the deepest value.  I'm trying to learn recursion better, but I'm not sure what I'm doing wrong.</p>