Find directories containing a certain number of files
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}'
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.
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.