Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers 2

up vote 2 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
add comment

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)... ...so your expression can be veeeeery loooong. :) –  september Jul 8 '13 at 22:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.