Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to find first match in multiple files

Is there a way for the find command to search for the first match or occurrence of a string or pattern within each of multiple files? I've been using the usual syntax:
 find dir -iname '*.ext' -exec command 'pattern' {} \;


Keep in mind this is not same as the commonly-asked problem of producing the first result from a search using find with -quit or head -n 1.
by

1 Answer

Amit8z4mc
Just use the -m option of GNU grep which stops reading the file after (in the example) one match.
find dir -iname '*.ext' -exec grep -m 1  'pattern' {} \;

Login / Signup to Answer the Question.