grep command
Search text for lines that match a pattern.
- Text processing
- Package grep
- 11 options · 11 examples
Syntax
grep [options] pattern [file…]Provided by the grep package. Run man grep for the full manual.
Common options
| Option | What it does |
|---|---|
-i | Ignore case |
-r | Search directories recursively |
-n | Show line numbers |
-v | Show lines that don't match |
-w | Match whole words only |
-c | Count matching lines |
-l | Only list file names with a match |
-E | Extended regex (|, +, ? without escaping) |
-o | Print only the matched part |
-A N / -B N / -C N | Show N lines after / before / around each match |
--include=GLOB | Only search matching file names |
Examples
Search a file
grep "error" /var/log/syslogIgnore case
grep -i "warning" app.logSearch a directory recursively
grep -rn "DB_PASSWORD" /var/www/Only list files that match
grep -rl "TODO" src/Lines that do not match
grep -v "^#" /etc/ssh/sshd_configCount matches
grep -c "404" access.logSeveral words at once
grep -E "error|fail|critical" app.logShow 3 lines around each match
grep -C 3 "Exception" app.logOnly in .php files
grep -rn --include="*.php" "mysqli_connect" .Remove comments and blank lines
grep -Ev "^\s*(#|$)" nginx.confPrint only the matched text
grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.log
No example matches. Check the options table or search the guides.
Tips
egrepis the same asgrep -Eand is deprecated.
grep guides
- Grep Command in Linux (Search Text in Files) Linux Commands · Updated Apr 2025
- A Comprehensive Guide to Grep Multiple Words from Files Linux Commands · Updated Apr 2025
- How to Grep for Contents after a Matching Pattern Linux Commands · Updated Apr 2025
- Grep Content Between Two Matching Patterns in Linux Linux Commands · Updated Apr 2025
- How to Show Line Numbers with the Grep Command Linux Commands · Updated Apr 2025
- Grep: Display N Lines Before and After Your Search Matches Linux Commands · Updated Apr 2025
