How to Search Text With grep in Linux

grep searches input (files or piped text) for lines matching a pattern and prints those lines.

Published September 30, 2026

grep searches text for lines matching a pattern. It is the standard tool for finding strings in files and filtering command output.

How it works

Patterns can be fixed strings or regular expressions. Useful flags: -i (ignore case), -r (recursive), -n (line numbers), -v (invert) and -E (extended regex).

It is often used at the end of a pipe.

Try it

grep -rn "TODO" src/
src/app.js:12: // TODO fix this
src/db.js:40: // TODO index

Practical examples

Recursive search with line numbers

grep -rn "error" /var/log/

Ignore case and show counts

grep -ic "warning" app.log

Filter command output

ps aux | grep nginx

Invert match

grep -v "^#" config.conf

Common mistakes

  • Forgetting quotes around patterns.
  • Matching the grep process itself in ps | grep.
  • Using basic regex when you need -E.

Key takeaways

  • grep finds matching lines.
  • -r -n -i -v are the key flags.
  • Quote your patterns.

FAQ

grep vs ripgrep?

ripgrep (rg) is faster and respects .gitignore.

Search for a whole word?

grep -w word.

More Linux articles