← Knowledge Base
LinuxReference note1 min read · updated 2026-11-27

Search text with grep: the useful flags

Recursive search, context lines, inverted matches, counting and regular expressions with grep.

grepregexcli
grep 'error' app.log                  # lines containing "error"
grep -i 'error' app.log               # ignore case
grep -n 'error' app.log               # show line numbers
grep -v 'debug' app.log               # lines that do NOT match
grep -c 'error' app.log               # count matching lines
grep -r 'TODO' src/                   # search a directory tree
grep -rl 'password' /etc 2>/dev/null  # only the names of matching files
grep -w 'port' conf                   # whole word only
grep -E 'error|warn|fatal' app.log    # extended regex (alternatives)
grep -A3 -B1 'Traceback' app.log      # 3 lines after, 1 before

Use -F for a plain string without regex meaning (grep -F '1.2.3' file). rg (ripgrep) is a faster alternative for large code trees.