linux - Recursive Function to Return Directory Depth of File Tree -
i'm trying write function traverse file directory , give me value of deepest directory. i've written function , seems going each directory, counter doesn't seem work @ all.
dir_depth(){ local olddir=$pwd local dir local counter=0 cd "$1" dir in * if [ -d "$dir" ] dir_depth "$1/$dir" echo "$dir" counter=$(( $counter + 1 )) fi done cd "$olddir" }
what want feed function directory, /home, , it'll go down each subdirectory within , find deepest value. i'm trying learn recursion better, i'm not sure i'm doing wrong.
here version seems work:
#!/bin/sh dir_depth() { cd "$1" maxdepth=0 d in */.; [ -d "$d" ] || continue depth=`dir_depth "$d"` maxdepth=$(($depth > $maxdepth ? $depth : $maxdepth)) done echo $((1 + $maxdepth)) } dir_depth "$@"
Comments
Post a Comment