terminal.cab

Linux Basics Lesson 9 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Finding lines, finding differences

10 minutes

Learning goals

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:

  • -i ignore upper and lower case
  • -v the lines that do not match
  • -c only the number of matching lines

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 5000ms

in the glossary: grep

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

  • -l lines only

Example:

wc -l logs/app.log
17 logs/app.log
grep ERROR logs/app.log | wc -l
2

in the glossary: wc

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

  • -u unified format with context, the one git diff uses too

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 = 30

in the glossary: diff

Without -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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Predict the output

Command

grep -c retry logs/app.log

Before the command, in your home folder ~

  • ~/logs/app.log
What does grep -c retry logs/app.log print?

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

  1. Question 1 of 3Some programs write Error, others ERROR. Which option finds both?

    Check works once you have chosen an answer.

  2. Question 2 of 3diff a.conf b.conf prints nothing at all. What does that tell you?

    Check works once you have chosen an answer.

  3. Question 3 of 3Which pattern finds only the lines that begin with 09:41?

    Check works once you have chosen an answer.

Cards

  1. Card 1 of 3

    Tap the card or press Space to turn it over.

  2. Card 2 of 3

    Tap the card or press Space to turn it over.

  3. 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

Next: lesson 10, Sort and count (10 min)

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

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: open
  6. Lesson 6: open
  7. Lesson 7: open
  8. Lesson 8: open
  9. Lesson 9: done
  10. Lesson 10: open
  11. Lesson 11: open
  12. Lesson 12: open
  13. Lesson 13: open
  14. Lesson 14: open
  15. Lesson 15: open
  16. Lesson 16: open
  17. Lesson 17: open
  18. Lesson 18: open
  19. Lesson 19: open
  20. Lesson 20: open
  21. Lesson 21: open
  22. Lesson 22: open
  23. Lesson 23: open
  24. Lesson 24: open
  25. Lesson 25: open
  26. Lesson 26: open
  27. Lesson 27: open
  28. Lesson 28: open
  29. Lesson 29: open
  30. Lesson 30: open
  31. Lesson 31: open
  32. Lesson 32: open
  33. Lesson 33: open
  34. Lesson 34: open
  35. Lesson 35: open
  36. Lesson 36: open
  37. Lesson 37: open
  38. Lesson 38: open
1 of 38

Next: lesson 10, Sort and count (10 min) That's enough for today

Path complete

You finished all 38 lessons of Linux Basics.

Back to the path

Cheat sheet

The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.