I have a directory which contains a number of subdirs. Each of these subdirs again contains a number of subdirs (call them subsubdirs). Now I would like to count the average number of subsubdirs in the top level directory.
Average number of subdirs
files;directory
This script uses awk to extract sub-directories from the ls list. Each subdirectory is entered and it's subdirectories counted. Finally the average calculation uses dc. I've set dc (using 2 k)  to output to 2 decimal places.dirs=$(ls -ld * | awk '$1 ~ /^d.*/ { print $9 }')ndirs=0for d in $dirsdo    cd $d    current_nsubdirs=$(ls -ld * 2>/dev/null | awk '$1 ~ /^d.*/ { print $9 }' | wc -l)    nsubdirs=$(($nsubdirs + $current_nsubdirs))    ndirs=$(($ndirs + 1))    cd ..doneecho Total subdirs $ndirsecho Total subsubdirs $nsubdirsavg=$(dc <<< 2 k $nsubdirs $ndirs / p)echo Average subsubdirs $avg