listing directories with more than x files

How can I find and print the directories on a server that have more than 5,000 files? There's some spam emails and I'm trying to find all directories that have a lot of spam

The file count should just be the files directly under that directory, not like the total from all nested directories

thanks :slight_smile:

Got lots of time? this will take a while. There a lot of kinds of files, too.

find / -type d > /tmp/dir.txt
while read dir 
do
    cnt=$(ls $dir | wc -l)
    [ $cnt -gt 5000 ] && echo $dir
done < /tmp/dir.txt > /tmp/big_directories.txt

This does not take into account symbolic links or pipes or sockets.