Use grep -m 1 to grep stop after searching for the first match in the file. It is extremely effective for large text files.
grep -m 1 str1 * /dev/null | head -1
If there is one file, then / dev / null above ensures that grep prints the file name in the output.
If you want to stop after searching for the first match in any file:
for file in *; do if grep -q -m 1 str1 "$file"; then echo "$file" break fi done
The for loop also eliminates the problem of too many arguments when you have a directory with many files.
codeforester
source share