Is there a way to return a list of all folders on a given disk containing more than X images ?

The search should be recursive but X should not.

Example for X=100 :

Folder A
|_ folder A1 (containing 100 pictures)
|_ folder A2 (containing 50 pictures)
|_ 50 pictures

Should return "Folder A1" only.

2 Answers 2


I am assuming all the pictures end with .png and X=100

find ./ -type d -exec sh -c 'count=$(ls "${0}"/*.png | wc -l); if [ "${count}" -ge 100 ];then echo "${0}"; fi ' {} \;


It can be extended for files for any number of extensions and any value of X like below

export files='png jpg gif'
export X='100'
find ./ -type d -exec \
sh -c 'count=$(for i in ${files}; do ls "${0}"/*.${i};done | wc -l); \
if [ "${count}" -ge "${X}" ];then echo "${0}"; fi ' {} \;

for each image, print the name of the directory it lies in:

find \( -name \*.png -or -name \*.gif -or -name \*.jpg \) -printf '%h\n' |

uniq needs sorted input, but find prints files rather randomly. (In my test, even find -depth didn't help, don't ask me why.)

sort |

for every directory name, tell us how often it occurs:

uniq -c |

and finally, filter the lines we want:

while read count dir; do
    if [ "$count" -ge 100 ]; then echo "$count $dir"; fi

You must log in to answer this question.

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