Find Number Of Files In A Directory

To get a list of all sub directories in a directory, with the number of files each directory contains:

find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \;

To do this recursively, that is, to get a list of all sub-directories in a directory, and a list of the sub-directories within those directories, with the number of files each directory contains:

find . -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \;

To select those directories that have exactly 5 files:

find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | 
  awk '$NF==5'


To find those sub directories with 5 or more files:

find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | 
awk '$NF>=10'


To find those subdirectories with 5 or fewer files:

find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | 
awk '$NF<=10'


To find completely empty directories (you may want to do this, for example, when creating backup scripts, as it is easier to check if a directory is empty, than to check whether a zero size file is supposed to be zero bytes or not):

[code]find . -maxdepth 1 -type d -exec bash -c "echo -ne '{} '; ls '{}' | wc -l" \; | 
awk '$NF<1'