grep
Search text using patterns
By CMD Script Team · 3 min read · Last updated
grep [OPTIONS] PATTERN [FILE...]Options
| Flag | Description |
|---|---|
-i | Case-insensitive match |
-n | Show line numbers for matches |
-v | Invert the match: print lines that do NOT match |
-r | Search directories recursively |
-E | Use extended regular expressions (same as egrep) |
-c | Print only a count of matching lines per file |
-l | Print only the names of files with at least one match |
-C | Show 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 matchgrep -n "TODO" main.py— show line numbersgrep -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
grepuses 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
-iwhen 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-rinstead. - Piping
lsintogrepto filter filenames instead of usingfindor shell globbing.
Tips
- Use
grep --color=autoto highlight matches in the terminal. - Combine
-owith a regex to extract just the matching text instead of the whole line. grep -qsuppresses 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/FIXMEcomments before a release. - Filtering
ps auxoutput to find a specific running process:ps aux | grep nginx.
Common interview questions
- What's the difference between
grep,egrep, andfgrep?egrepisgrep -E(extended regex);fgrepisgrep -F(literal string matching, no regex). - How do you search recursively while ignoring binary files?
grep -rI pattern .—-Iskips 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.