How do I count all the files recursively through directories

List folders with file count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” -type f | wc -l); printf “%4d : %s\n” $n “$dir”; done
List folders with non-zero file count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” -type f | wc -l); if [ $n -gt 0 ]; then printf “%4d : %s\n” $n “$dir”; fi; done
List folders with sub-folder count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” -type d | wc -l); let n–; printf “%4d : %s\n” $n “$dir”; done
List folders with non-zero sub-folder count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” -type d | wc -l); let n–; if [ $n -gt 0 ]; then printf “%4d : %s\n” $n “$dir”; fi; done
List empty folders:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” | wc -l); let n–; if [ $n -eq 0 ]; then printf “%4d : %s\n” $n “$dir”; fi; done
List non-empty folders with content count:

find -maxdepth 1 -type d | sort | while read -r dir; do n=$(find “$dir” | wc -l); let n–; if [ $n -gt 0 ]; then printf “%4d : %s\n” $n “$dir”; fi; done
shareimprove this