Ubuntu – Recursively search a pattern/text only in the specified file name of a directory

command linefindgrep

I have a directory (e.g., abc/def/efg) with many sub-directories (e.g.,: abc/def/efg/(1..300)). All of these sub-directories have a common file (e.g., file.txt). I want to search a string only in this file.txt excluding other files. How can I do this?

I used grep -arin "pattern" *, but it is very slow if we have many sub-directories and files.

Best Answer

In the parent directory, you could use find and then run grep on only those files:

find . -type f -iname "file.txt" -exec grep -Hi "pattern" '{}' +
Related Question