grep Command Guide for Searching Text
This guide helps you understand and use the grep command, the standard way to find lines matching a pattern inside text.
What is grep, and Why Use It?
The grep command searches one or more files (or a stream) for lines matching a pattern, and prints every line that matches. The name comes from the old ed editor command g/re/p — “global, regular expression, print.”
Why Learn grep?
- Find anything in logs: Spot errors, usernames, or IP addresses in files with thousands of lines.
- Filter command output:
lsoutput, process lists, command help — anything can be piped throughgrep. - It’s everywhere: Scripts, config audits, security checks —
grepis the first tool most sysadmins reach for. - Patterns scale: What starts as “find this word” grows into full regular expressions that describe shapes of text.
If you learn only one command from this section, make it
grep. It is the most-used text tool in existence.
1. Search a Single File
grep "error" /var/log/syslog
- Prints every line in the file containing the word
error. - The match is highlighted in color when output goes to a terminal.
Example:
grep "error" /var/log/syslog
error: disk full on /dev/sda1
error: cannot open config file
2. Search Multiple Files
grep "error" /var/log/syslog /var/log/kern.log
- When searching more than one file,
grepprefixes each match with the filename.
Tip: search every file in a directory with a glob:
grep "TODO" src/*.md
3. Search a Whole Directory Recursively
grep -r "TODO" src/
-r(recursive) descends into every file under the directory.- This is the most common form of
grepin daily work.
| Flag | Effect |
|---|---|
-r | Search directories recursively. |
-l | List only the names of files containing a match. |
-L | List only files that do not contain a match. |
-c | Print a count of matching lines per file. |
-n | Print line numbers along with matches. |
Example — which files mention a setting, and how often?
grep -rc "timeout" /etc/nginx/
4. Case-Insensitive and Inverted Search
grep -i "error" file.txt
-iignores case, soError,ERROR, anderrorall match.
grep -v "debug" file.txt
-vinverts the match: print every line that does not match. Useful for stripping out noise:
grep -v "^#" config.txt
That prints the config without comment lines.
5. Whole Words, Context, and Colors
grep -w "cat" file.txt
-wmatches whole words only, socatno longer matchesconcatenate.
Context around each match:
grep -B 2 -A 2 "panic" kernel.log
-B 2prints 2 lines Before each match,-A 2prints 2 lines After.-C 3prints 3 lines both before and after.
Force colored output when piping to another command:
grep --color=always "error" file.txt | less -R
6. Regular Expressions
grep accepts three levels of pattern matching:
grep "user[0-9]" file.txt # basic: digit ranges
grep -E "user[0-9]+" file.txt # extended: one-or-more, groups, alternation
| Pattern | Matches |
|---|---|
. | Any single character. |
* | Zero or more of the previous character. |
^ | Start of line. |
$ | End of line. |
[abc] | Any one character in the set. |
[^abc] | Any one character not in the set. |
| (with -E) | Alternation: this or that. |
Examples:
grep -E "^(alice|bob):" /etc/passwd # users alice or bob
grep -E "^[0-9]{3}-[0-9]{4}" data.txt # phone-number-like lines
grep "error$" file.txt # lines ending in 'error'
7. Search for Literal Patterns and Binary Files
grep -F "192.168.1.1*" file.txt
-Ftreats the pattern as a fixed string, not a regex — faster and safer when your pattern contains characters like.or*.
grep -I "pattern" *
-Iskips binary files entirely (the uppercasei). Without it,grepon a binary prints “Binary file … matches” instead of the lines.
8. Combine with Pipes
grep shines when filtering the output of other commands:
ps aux | grep "nginx"
ls -l | grep -c "^-"
That counts regular files in a listing. The classic anti-pattern ps aux | grep foo also matches the grep itself — use pgrep instead when you only want process IDs (see ps in the processes section).
Common Pitfalls
- Forgetting to quote patterns with spaces or special characters.
grep error: file not foundfails; writegrep "error" file.txt. - Confusing regex and literal matching.
grep "file.txt"treats.as “any character”. Use-Ffor literal strings. grepmatches substrings by default. Searching forcatalso findsconcatenate. Add-wfor whole words.- Case surprises.
grep Errormisseserror. Use-iunless case matters. - Binary files silently matching. Add
-Ior--binary-files=without-matchwhen searching everything under a directory.
Related Pages
| Page | Relationship |
|---|---|
sed | Edits and replaces text instead of just finding it. |
awk | Analyzes structured columns instead of matching whole lines. |
pipes.md | How grep receives data from other commands. |
egrep/fgrep | Legacy names for grep -E and grep -F. |
grep Cheatsheet
| Command | What It Does |
|---|---|
grep "word" file | Print lines containing word. |
grep -r "word" dir/ | Search a directory recursively. |
grep -i "word" file | Case-insensitive search. |
grep -v "word" file | Print lines that do not match. |
grep -n "word" file | Show line numbers. |
grep -c "word" file | Count matching lines. |
grep -l "word" * | List only filenames with matches. |
grep -w "word" file | Whole-word match only. |
grep -E "a|b" file | Extended regex: a or b. |
grep -F "str" file | Treat pattern as a literal string. |
grep -A3 -B3 "x" f | Show 3 lines of context around matches. |
grep -I "word" * | Skip binary files. |