Find directories that contains more than n matches of a certain filename

I need to construct a command that finds directories which contains more than n matches of a certain filename.

E.g. I have many directories at different locations and want to find all directories that has 2 or more .dat-files.

I thought of using find and maybe the exec parameter to issue an ls|wc -l, but that would just return the number of files, not the directory itself (that i need).

Any suggestions?

Hi, welcome to the forum. You could use an awk command to cut off after the last / and count the find output, for example:

find . -name '*.dat' | awk -F/ '{NF--}++A[$0]==n' OFS=/ n=2

Hi Scrutinizer,

I little new to UNIX.. Could you please explain the below awk command posted by you in detail ???

Thanks in advance!
Vasanth.

(Remove the wrong statement.)

Below code will be easy for beginner for understanding.

find . -name '*.dat' -type f -exec dirname {} \; |sort |uniq -c |awk '$1>n' n=2

No, I think my code is correct. It needs to be == , otherwise the same directory gets printed multiple times. Put in another way, once the number of occurrences reaches 2, it answers the criterion of 2 or more .dat files and the directory should be printed, no matter how many more .dat files are found in the lines that follow..