grep -m: Stop After N Matches in CI
grep -m N stops reading after it has found N matching lines.
On a multi-gigabyte log you rarely need every match. -m lets grep quit early, which is faster and enough to answer "did this happen at least once".
What it does
grep -m N (--max-count) prints at most N matching lines per file and stops scanning that file once N is reached. When reading from stdin or a single file, this means grep can exit long before consuming the whole input, which is a real speedup on large logs.
Common usage
# just the first error
grep -m 1 ERROR huge.log
# does it appear at all (fast, quiet)
grep -qm 1 "Listening on" server.log
# first 5 matches for a quick sample
grep -m 5 "deprecated" build.logOptions
| Flag | What it does |
|---|---|
| -m N / --max-count=N | Stop after N matching lines per file |
| -q | Combine for a fast presence check |
| -o | With -m, limit how many matches print |
| -H | Show filename when scanning several files |
In CI
For a "did it happen" check on a large or streaming log, grep -qm 1 exits the instant it finds the first match, which is the fastest possible presence test. The exit code still follows the match rule: 0 if it hit the pattern, 1 if it reached end of input first.
Common errors in CI
On a streaming pipe, grep -m 1 closing early can send SIGPIPE to the producer, which may log "Broken pipe" or return non-zero under set -o pipefail; that is expected from the early exit, not a grep fault. If the upstream command must finish, drop -m or capture its output first.