>_cmd.script

grep

Search text using patterns

Text Processing

By CMD Script Team · 3 min read · Last updated

SYNTAX
grep [OPTIONS] PATTERN [FILE...]

Options

Command options and flags
FlagDescription
-iCase-insensitive match
-nShow line numbers for matches
-vInvert the match: print lines that do NOT match
-rSearch directories recursively
-EUse extended regular expressions (same as egrep)
-cPrint only a count of matching lines per file
-lPrint only the names of files with at least one match
-CShow N lines of context around each match, e.g. -C 3

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS (BSD grep, some flags differ)

What it does

grep searches input — files or stdin — for lines matching a pattern and prints the matching lines. The name comes from the ed editor command g/re/p ("global regular expression print"). It's one of the most-used tools for searching logs, source code, and configuration files from the command line.

Beginner examples

  • grep "error" app.log — print lines containing "error"
  • grep -i "error" app.log — case-insensitive match
  • grep -n "TODO" main.py — show line numbers
  • grep -v "debug" app.log — print lines that do NOT match
grep -in "warning" /var/log/syslog

Advanced examples

  • Search recursively through a directory: grep -r "TODO" ./src
  • Use extended regex for alternation: grep -E "error|fatal" app.log
  • Show 3 lines of context around each match: grep -C 3 "exception" app.log
  • Count matches instead of printing lines: grep -c "error" app.log
  • Search multiple files and show only filenames with matches: grep -l "TODO" src/*.py
grep -rl "console.log" ./src

Common mistakes

  • Forgetting that grep uses basic regular expressions by default, so +, ?, and | need escaping (or use -E/egrep).
  • Searching case-sensitively by accident and getting zero results — sanity-check with -i when a search seems to come up empty.
  • Running grep pattern * in a directory with subdirectories and missing matches inside them, because the shell glob doesn't recurse — use -r instead.
  • Piping ls into grep to filter filenames instead of using find or shell globbing.

Tips

  • Use grep --color=auto to highlight matches in the terminal.
  • Combine -o with a regex to extract just the matching text instead of the whole line.
  • grep -q suppresses output and only sets an exit code — handy in shell scripts (if grep -q pattern file; then ... fi).

Best practices

  • Anchor patterns with ^/$ when you mean "starts with" or "ends with" to avoid unintended partial matches.
  • Prefer grep -F (fixed string) over regex when searching for a literal string with special characters — faster and avoids escaping bugs.
  • In scripts, always check grep's exit code rather than assuming success; it returns non-zero when no lines match.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Searching application logs for error patterns during an incident.
  • Auditing a codebase for leftover TODO/FIXME comments before a release.
  • Filtering ps aux output to find a specific running process: ps aux | grep nginx.

Common interview questions

  • What's the difference between grep, egrep, and fgrep? egrep is grep -E (extended regex); fgrep is grep -F (literal string matching, no regex).
  • How do you search recursively while ignoring binary files? grep -rI pattern .-I skips binary files.
  • How would you find lines that do NOT match a pattern? grep -v pattern file.

Frequently Asked Questions

How do I search recursively?

Use grep -r pattern directory/ to search every file under a directory.

What's the difference between grep, egrep, and fgrep?

egrep is grep -E (extended regex, supports + ? | without escaping); fgrep is grep -F (literal string matching, no regex).

How do I find lines that do NOT match a pattern?

Use grep -v pattern file.

Cheat sheet

Download a quick-reference cheat sheet for grep.