How to search with grep

Overview

grep prints lines matching a pattern. It can search one file, many files, or text received through a pipe.

Examples

grep 'error' app.log
grep -i 'ubuntu' notes.txt
grep -n 'TODO' *.txt
grep -R --include='*.py' 'print(' .
ps aux | grep firefox

Useful options

  • -i ignores letter case
  • -n prints line numbers
  • -v selects non-matching lines
  • -w matches whole words
  • -R searches directories recursively
  • -E enables extended regular expressions

Tips

Quote patterns to protect spaces and special characters from the shell. Start with literal words before learning regular expressions.

A status of 1 often simply means no lines matched; it is not necessarily a failure.