Linux Command Line · Lesson 16 of 43
Searching text with grep
Search files and command output with grep: match words, ignore case, count, invert, show context, search folders and use basic regular expressions.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
You will search any file, folder or command output for exactly the lines you need. grep is the most used text tool on Linux: you will reach for it when reading logs, hunting through code, filtering lists and checking configuration.
The basic form
grep takes a pattern and prints every line that contains it. The simplest pattern is a plain word:
cd ~/project
grep ERROR logs/app.log2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeoutQuote patterns that contain spaces or special characters, such as grep "user asha" logs/app.log. With no file name, grep reads from standard input, so it works at the end of any pipeline.
Everyday options
Add -n to show line numbers, which is handy for finding the place in the file:
grep -n WARN logs/app.log3:2026-01-15 10:01:12 WARN disk usage at 81%Ignore case with -i, and invert the match with -v to see everything except the matching lines:
grep -i "buy" docs/tasks.txt
grep -v INFO logs/app.logBuy milk
Buy bread
2026-01-15 10:01:12 WARN disk usage at 81%
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeoutCount matching lines with -c, and match a whole word with -w, so that Ben does not match inside Benjamin:
grep -c INFO logs/app.log
grep -w blue data/scores.csv5
Asha,blue,82
Chitra,blue,91Context around a match
A matching line is often useless without the lines near it. -A shows lines after, -B lines before, and -C both:
grep -B1 -A1 "database timeout" logs/app.log2026-01-15 10:01:12 WARN disk usage at 81%
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:41 INFO retrying connection
2026-01-15 10:02:44 ERROR database timeout
2026-01-15 10:03:00 INFO connection restoredMatches are separated by -- lines when their context does not touch.
Searching many files
To search every file in a folder tree, use -r. -l prints only the names of files with a match, and -n adds line numbers when searching:
grep -rn "echo" src
grep -rl "Linux" .src/hello.sh:2:echo "Hello from a script"
./docs/notes.txtWhen you search a whole code base, add --include="*.js" to limit the search to certain files, or --exclude-dir=node_modules to skip a folder.
Showing only the match
-o prints just the matching part of each line, one per line, which is excellent for extracting values:
grep -o "user [a-z]*" logs/app.loguser asha
user benRegular expressions
A pattern is a regular expression: a small language for describing text. The common pieces are:
| Pattern | Meaning |
|---|---|
^ |
start of a line |
$ |
end of a line |
. |
any single character |
* |
zero or more of the previous item |
[abc], [0-9] |
one character from a set or range |
[^abc] |
one character not in the set |
\ |
take the next character literally |
grep "^Buy" docs/tasks.txt
grep "s\.$" docs/notes.txt
grep "ba.ana" data/fruit.txtBuy milk
Buy bread
Linux is a family of operating systems.
The shell reads your commands.
Commands are small programs.
banana
bananaNotice that this * is not the same as the star in file wildcards: here it means "repeat the previous item".
Extended expressions
Plain grep treats +, ?, | and parentheses as ordinary characters. grep -E (extended) gives them their special meaning:
grep -E "ERROR|WARN" logs/app.log
grep -E "^[A-Z][a-z]+,blue" data/scores.csv2026-01-15 10:01:12 WARN disk usage at 81%
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeout
Asha,blue,82
Chitra,blue,91-E "ERROR|WARN" means "ERROR or WARN". BSD and macOS grep also support -E, but the more advanced -P (Perl patterns) is a GNU option.
Exit status
grep exits with 0 if it found something, 1 if not and 2 on error. Scripts use this directly, as in if grep -q ERROR file; then ..., where -q suppresses the output.
Common mistakes
Forgetting to quote the pattern. Characters like *, $ and | are meaningful to the shell, so quote the pattern.
Mixing wildcards and regular expressions. grep *.log does not mean "log files". Use ls *.log | ... or grep -r pattern --include="*.log".
Not using -i. Searching for error misses ERROR.
Expecting a match on part of the file name. grep searches contents, not names. Use find for names.
Exercise
In logs/app.log, show the lines with ERROR together with their line numbers, then count the lines that are not INFO. Use -o to print only the user names from the login lines. Finally, use -E to show lines with either WARN or ERROR, and -C1 to show context around the first error.
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.