Last updated
Quick Reference: Common Flags
-i— case-insensitive search-r— recursive (search subdirectories)-n— show line numbers-l— show only filenames with matches-L— show only filenames WITHOUT matches-c— show count of matches per file-v— invert match (show non-matching lines)-w— whole word match only-o— show only the matching part-E— extended regex (enables +, ?, |, ())-P— Perl-compatible regex (enables \d, \w, lookaheads)-A N— show N lines after match-B N— show N lines before match-C N— show N lines before and after match
Examples
Example 1: Basic Text Search
# Search for exact text in a file
grep "error" app.log
# Case-insensitive search
grep -i "error" app.log
# Search in all files in current directory
grep "TODO" *.js
# Recursive search through all subdirectories
grep -r "TODO" src/
# Recursive, case-insensitive, with line numbers
grep -rin "todo" src/
Example 2: Word Boundary Search
# Match whole word only (not substrings)
grep -w "log" app.js
# Matches: log, but NOT: logger, catalog, dialog
# Using word boundary in regex
grep "\blog\b" app.js
# Find exact variable name (not partial matches)
grep -w "count" src/utils.js
# Matches: count = 0, but NOT: accountId, discount
Example 3: Line Anchors
# Lines starting with "import"
grep "^import" src/index.js
# Lines ending with semicolon
grep ";$" src/app.js
# Lines that are exactly "}"
grep "^}$" src/component.jsx
# Empty lines
grep "^$" config.yaml
# Non-empty lines
grep -v "^$" config.yaml