I have a directory, containing a lot of files and directories.I am trying to get the number of files (and directories) contained recursively in every directory.I tried the following approach:for dir in $(find -maxdepth 1 -type d); do echo $dir; echo find $dir | wc -l; doneBut this returns 1 as result for every directory.I know there are several other questions with a similar question, but I would really like to know what my mistake is in the code above.
How to count recursively for the number of files in several directories?
files;find
A GNU (bash, wc and find) solution which works with any path, even those containing spaces, newlines or starting with a dash:shopt -s nullglobfor dir in ./*/do    printf '%s\n' $dir    find $dir -mindepth 1 -printf x | wc --charsdoneExplanation:The nullglob option prevents errors if ./ contains no directories.The ./ in the directory glob ensures that file names starting with a dash (-) won't mess up echo or find.The slash at the end of the glob ensures that only directories are processed.-mindepth 1 avoids counting the directory itself.If you want to include directories which start with a dot on the top level, you should run shopt -s dotglob before the for loop.