Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
try something like "ls -al | wc -l | grep" – Vanadis Jul 8 '13 at 17:13
up vote 5 down vote accepted

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}'
share|improve this answer
I think it might be useful to include the awk command to cut off the file count (i.e. the last whitespace-delimited column), in case the questioner wants to pipe the output to something else. – evilsoup Jul 8 '13 at 20:09
@evilsoup good idea, done. – terdon Jul 8 '13 at 20:51

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.

share|improve this answer
Your solution also counts the current directory (.), you may want to modify accordingly. – terdon Jul 8 '13 at 18:38
I like the thrust of this answer (because I'm a glutton for doing things in the shell), but you'd be better off using wc -l < <(printf %s\\n ./*) or printf %s\\n ./* | wc -l inside the test, to avoid an unnecessary find call. This will also avoid the problem that @terdon noted, of including . in the result. However, it would also run into the problem of ignoring files beginning with a .; I would solve this with shopt -s dotglob (to make globs match files beginning with a ., but not . or ..). – evilsoup Jul 8 '13 at 20:23
@terdon It is not important. It is not final solution, just example, idea. You can -1, or change 10 to 11 in final version. – september Jul 8 '13 at 21:49
I know, and the idea is good that's why I made the suggestion. – terdon Jul 8 '13 at 22:02
@ terdon. Thank you. There can be many different requirements, like: Count only files but not not directories, or links, or hard links. Count or not files in subdirectories. Count hidden files (like .bashrc)... your expression can be veeeeery loooong. :) – september Jul 8 '13 at 22:16

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .