Skip to lesson

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.

Beginner to intermediate·14 min·Text, redirection and pipes·Updated 2026-11-24

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:

bash
cd ~/project
grep ERROR logs/app.log
result
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeout

Quote 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:

bash
grep -n WARN logs/app.log
result
3: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:

bash
grep -i "buy" docs/tasks.txt
grep -v INFO logs/app.log
result
Buy 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 timeout

Count matching lines with -c, and match a whole word with -w, so that Ben does not match inside Benjamin:

bash
grep -c INFO logs/app.log
grep -w blue data/scores.csv
result
5
Asha,blue,82
Chitra,blue,91

Context around a match

A matching line is often useless without the lines near it. -A shows lines after, -B lines before, and -C both:

bash
grep -B1 -A1 "database timeout" logs/app.log
result
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:41 INFO retrying connection
2026-01-15 10:02:44 ERROR database timeout
2026-01-15 10:03:00 INFO connection restored

Matches 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:

bash
grep -rn "echo" src
grep -rl "Linux" .
result
src/hello.sh:2:echo "Hello from a script"
./docs/notes.txt

When 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:

bash
grep -o "user [a-z]*" logs/app.log
result
user asha
user ben

Regular 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
bash
grep "^Buy" docs/tasks.txt
grep "s\.$" docs/notes.txt
grep "ba.ana" data/fruit.txt
result
Buy milk
Buy bread
Linux is a family of operating systems.
The shell reads your commands.
Commands are small programs.
banana
banana

Notice 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:

bash
grep -E "ERROR|WARN" logs/app.log
grep -E "^[A-Z][a-z]+,blue" data/scores.csv
result
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 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.

0 / 4 understood
01 What does grep -v ERROR file print?
02 Which option makes grep search all files under a folder?
03 What does the pattern ^Buy match?
04 What does grep -c ERROR logs/app.log print?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses