grep

The command that prints the lines of its input that match a pattern.

In more detail

The pattern is a regular expression, so quote it and remember that a dot matches any character; -F searches for fixed text instead. -i ignores case, -v prints non-matching lines, -c counts, -n adds line numbers, -o prints only the match, -r searches a directory tree and -E switches to extended syntax. grep exits 0 when something matched, 1 when nothing did and 2 on an error, which makes it a natural test in if.

Examples

grep -rn -e found notes
grep -E ' [45][0-9]{2} ' access.log
grep -Eq "^(feat|fix): .+"
git log --grep searches commit messages the same way.
curl -s http://localhost:8002/metrics | grep -v duration_seconds
grep -rlF searches a whole directory tree for a fixed string and lists the files that contain it, which is a quick sweep for a dropped file path.

Related terms

Taught in

References

Certification courses