Linux Basics Lesson 9 of 38
Finding lines, finding differences
10 minutes
Learning goals
- Filter a log with grep and its options -i, -v, -n, -c, -r, -w and -E.
- Pin a pattern to the start or end of a line with ^ and $.
- Answer every "how many?" question with wc -l at the end of a pipe.
- Compare a config with its backup using diff and diff -u.
Finding lines: grep
grep PATTERN FILE prints every line that contains the pattern, and nothing else. -n adds the line number, so you can find the
place again later:
grep -n ERROR logs/app.log
Purpose: Prints the lines that contain a pattern. The most-typed filter there is.
Syntax: grep [OPTIONS] PATTERN [FILE...]
Options:
Example:
grep -n ERROR logs/app.log
9:09:27:15 ERROR db timeout after 5000ms
16:09:41:09 ERROR db timeout after 5000ms
grep -c ERROR logs/app.log
2
grep -v INFO logs/app.log
09:19:26 WARN slow query took 1840ms
09:27:15 ERROR db timeout after 5000ms
09:38:44 WARN slow query took 2210ms
09:41:09 ERROR db timeout after 5000msA few options you will use daily. -i ignores upper and lower case, because some programs write Error and others ERROR. -w
only matches whole words, so -w port does not hit export. -r searches a whole folder, and every line then starts with its
file name:
grep -i warn logs/app.log.1
23:52:08 WARN slow query took 1320ms
grep -r ERROR logs
logs/app.log:09:27:15 ERROR db timeout after 5000ms
logs/app.log:09:41:09 ERROR db timeout after 5000ms
grep -w retry logs/app.log
09:27:16 INFO retry ok
09:41:10 INFO retry ok
Pinning a pattern: ^, $ and -E
The pattern does not have to be a plain word. ^ stands for the start of a line and $ for its end. -E turns on extended
patterns, among them | for "or". Put quotes around such a pattern so that the shell keeps its hands off it:
grep '^09:41' logs/app.log
09:41:02 INFO request served in 84ms
09:41:09 ERROR db timeout after 5000ms
09:41:10 INFO retry ok
grep 'ok$' logs/app.log
09:27:16 INFO retry ok
09:41:10 INFO retry ok
grep -E 'WARN|ERROR' logs/app.log
09:19:26 WARN slow query took 1840ms
09:27:15 ERROR db timeout after 5000ms
09:38:44 WARN slow query took 2210ms
09:41:09 ERROR db timeout after 5000ms
How many? wc -l
wc counts. Most of the time you only want the lines, and that is -l. Hung onto the end of a pipe, it answers every "how many?"
question about whatever comes out on the left:
wc -l logs/app.log
Purpose: Counts lines, words and bytes. At the end of a pipe, -l answers every "how many?".
Syntax: wc [-lwc] [FILE...]
Options:
Example:
wc -l logs/app.log
17 logs/app.log
grep ERROR logs/app.log | wc -l
2grep -c gives you the same number here. The pipe works with every command, not just with grep.
Finding differences: diff
Before you restart a service with a changed config, compare it with the backup. diff prints nothing if both files are the same. -u
shows the differences with some context around them: - is what left, + is what came in. git diff uses the same format, so you
will read this for the rest of your life.
diff -u conf/app.conf.bak conf/app.conf
Purpose: Shows what differs between two files. No output means they are identical.
Syntax: diff [-u] FILE1 FILE2
Options:
Example:
diff -u conf/app.conf.bak conf/app.conf
--- conf/app.conf.bak 2026-10-16 17:00:00.000000000 +0000
+++ conf/app.conf 2026-10-17 08:00:00.000000000 +0000
@@ -1,4 +1,4 @@
port = 8080
-workers = 2
-log_level = info
+workers = 4
+log_level = debug
timeout = 30Without -u you get the older, terser form, where < is the first file and > the second:
diff conf/app.conf.bak conf/app.conf
2,3c2,3
< workers = 2
< log_level = info
---
> workers = 4
> log_level = debug
Practice
Terminal task 1/3
Mission
How many ERROR lines are there in logs/app.log? Only the number should end up on the screen.
Task 1/3: How many ERROR lines are there in logs/app.log? Only the number should end up on the screen.
Mission
How many ERROR lines are there in logs/app.log? Only the number should end up on the screen.
Task complete
Solution
grep -c ERROR logs/app.log Why: You could read the 17 lines here by eye, but on a real server it is a few hundred thousand. grep -c ERROR counts matching lines, not matches: two errors in one line count once. For most logs that is exactly what you want.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
The INFO noise hides what matters. Print all lines of logs/app.log that do not contain INFO, with their line numbers.
Task 2/3: The INFO noise hides what matters. Print all lines of logs/app.log that do not contain INFO, with their line numbers.
The INFO noise hides what matters. Print all lines of logs/app.log that do not contain INFO, with their line numbers.
Task complete
Solution
grep -vn INFO logs/app.log Why: -v is the quick way to get rid of noise. With the line numbers you find the spot again in less, by typing the number followed by g.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Yesterday somebody changed conf/app.conf. Compare the backup conf/app.conf.bak with it, in the format that git uses as well.
Task 3/3: Yesterday somebody changed conf/app.conf. Compare the backup conf/app.conf.bak with it, in the format that git uses as well.
Yesterday somebody changed conf/app.conf. Compare the backup conf/app.conf.bak with it, in the format that git uses as well.
Task complete
Solution
diff -u conf/app.conf.bak conf/app.conf Why: Now you know that two values changed, workers and log_level. Exit code 1 is not an error here. It means the files differ, and scripts can ask exactly that.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Predict the output
Command
grep -c retry logs/app.logBefore the command, in your home folder ~
- ~/logs/app.log
Check works once you have chosen an answer.
Fill the gap
How many lines of logs/app.log contain WARN? grep WARN logs/app.log |
How many lines of logs/app.log contain WARN? grep WARN logs/app.log | …
Check works once you have typed an answer.
Next up: sort, count and cut out columns, which turns a pile of lines into a statistic.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: grep -n ERROR logs/app.log, wc -l logs/app.log, diff -u conf/app.conf.bak conf/app.conf
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: done
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
grep -n ERROR <file>matching lines with line numbers -
grep -E 'WARN|ERROR' <file>one pattern or the other -
grep -r ERROR <folder>search a whole folder -
... | wc -lcount lines at the end of a pipe -
diff -u <old> <new>what is different, in the git format
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 73 KB)