Everyone else's solution has one drawback or another.
find -type d -readable -exec sh -c 'printf "%s " "$1"; ls -1UA "$1" | wc -l' sh {} ';'
Explanation:
-type d
: we're interested in directories.-readable
: We only want them if it's possible to list the files in them. Note that find
will still emit an error when it tries to search for more directories in them, but this prevents calling -exec
for them.-exec sh -c BLAH sh {} ';'
: for each directory, run this script fragment, with $0
set to sh
and $1
set to the filename.printf "%s " "$1"
: portably and minimally print the directory name, followed by only a space, not a newline.ls -1UA
: list the files, one per line, in directory order (to avoid stalling the pipe), excluding only the special directories .
and ..
wc -l
: count the lines