Definition
grep (global regular expression print) reads text input—files or stdin—and prints every line that matches a pattern. It is the workhorse for searching logs, configs, and source trees from the shell. Implementations differ slightly (GNU grep vs BSD grep), but the mental model is the same: pattern, optional flags, then paths.
Core flags
| Flag | Meaning | Typical use |
|---|---|---|
-i | Ignore case | Log keyword hunts |
-n | Line numbers | Editor jump targets |
-r / -R | Recursive directories | Repo-wide search (respect .gitignore with other tools) |
-E | Extended regex | Modern alternation and grouping |
-F | Fixed string | Literal IPv4 or dotted paths without escaping dots |
-v | Invert match | Drop noise lines |
--color | Highlight hits | Human readability in terminals |
Exit codes and pipelines
GNU grep exits 0 when a match exists, 1 when none, and 2 on error—scripts can branch on that. Piping journalctl | grep is common; remember grep may buffer unless you choose line-buffered modes for live tails.
Beyond classic grep
ripgrep (rg) respects ignore files and is faster on huge trees. git grep searches tracked content only. For structured logs, consider jq or Loki—not every problem is a regex problem.
Related: Linux grep command explained, Linux commands primer, Windows CMD commands (findstr counterpart).