The number of names in a directory may be counted using set -- *
. The count is then found in $#
.
With find
, you may execute the set
command and test the value of $#
in a short in-line shell script:
find . -type d -exec sh -c '
for dirpath do
set -- "$dirpath"/*
[ "$#" -gt 20000 ] && printf "%s\n" "$dirpath"
done' sh {} +
The inline sh -c
script will get batches of found directory pathnames as arguments. The script iterates over a batch of these pathnames and expands the *
glob in each. If the resulting list contains strictly more than 20000 strings, the directory pathname is outputted using printf
.
To also count hidden names, you may want to switch to calling the bash
shell with its dotglob
shell option set:
find . -type d -exec bash -O dotglob -c '
for dirpath do
set -- "$dirpath"/*
[ "$#" -gt 20000 ] && printf "%s\n" "$dirpath"
done' bash {} +
Note that if you want to do something to these directories, you'd be best served if you do that as part of the inline script above (rather than, say, try to parse the output of the find
command):
find . -type d -exec bash -O dotglob -c '
for dirpath do
set -- "$dirpath"/*
[ "$#" -gt 20000 ] || continue
# Process "$dirpath" here
done' bash {} +
find
command elsewhere, then change the.
afterfind
to the desired top-level directory. Do not modifydirpath
in the script, it's a variable that the script uses and its values will be provided byfind
. – Kusalananda Jan 24 '23 at 11:25find . -type d -exec sh -c 'for dirpath do set -- "$dirpath"/*; [ "$#" -gt 20000 ] && printf "%s\n" "$dirpath"; done' sh {} +
– Kusalananda Jan 24 '23 at 11:31