Find directories containing a certain number of files

Was hoping I would be able to do this with the find command but I can see no test in the manual for doing what I want. I would like to be able to find any directories in the working directory that contain less-than, more-than or exactly the count I specify.

find . -filecount +10 # any directory with more than 10 entries
find . -filecount 20 # any directory with exactly 20 entries

But alas there is no such option.


Solution 1:

You could try this, to get the sub directory names and the number of files/directories they contain:

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

If you want to do the same for all sub directories (recursive find) use this instead:

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

To select those directories that have exactly 10 files:

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

10 or more:

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

10 or less:

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

If you want to keep only the directory name (for example of you want to pipe it to another process downstream as @evilsoup suggested) you can use this:

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

Solution 2:

Terdon's answer is generally well illustrative of the right approach, however here's a slightly shorter answer which only uses find and bash (doesn't need ls, wc and awk), and can also deal with files or directories containing newlines.

find . -type d -exec /bin/bash -c 'a=( "{}"/* ); [[ ${#a[*]} == 10 ]]' ';' -print

Solution 3:

To list immediate subdirectories containing exactly $NUM files.

find -maxdepth 2 -mindepth 2 -type f -printf '%h\0' | awk -v num="$NUM" 'BEGIN{RS="\0"} {array[$0]++} END{for (line in array) if (array[line]==num) printf "%s\n", line}'

To list immediate subdirectories containing greater than $NUM files.

find -maxdepth 2 -mindepth 2 -type f -printf '%h\0' | awk -v num="$NUM" 'BEGIN{RS="\0"} {array[$0]++} END{for (line in array) if (array[line]>num) printf "%s\n", line}'

To list immediate subdirectories containing less than $NUM files.

find -maxdepth 2 -mindepth 2 -type f -printf '%h\0' | awk -v num="$NUM" 'BEGIN{RS="\0"} {array[$0]++} END{for (line in array) if (array[line]<num) printf "%s\n", line}'

Items are terminated by a null character \0, so file names that contain newlines or other types of white space will be interpreted correctly. The %h prints each file's dirname. awk then uses an array to count how many times it encounters each directory, printing it if the conditions are met.

Please note that none of the aforementioned commands will display directories containing zero files. Also note that by file I am referring to regular files, not links, directories, sockets, blocks, named pipes, etcetera.

I've tried to do this as simply as possible. If you want to find recursive subdirectories or the files therein, a modified command is required. There are too many possibilities to list them all.

Solution 4:

Try this:

[ `find . | wc -l` -eq 10 ] && echo "Found"

[ `find . | wc -l` -gt 10 ] && echo "Found"

[ `find . | wc -l` -lt 10 ] && echo "Found"

In this examples you can check if CURRENT directory contains exactly 10, more then 10 and less then 10 files/directories. If you need to check bunch of directories, just use loop.