Linux Basics Lesson 2 of 38
Reading logs without drowning
9 minutes
Learning goals
- Choose between cat, less, head and tail by the size of the file.
- Page and search in less, and always find the way out with q.
- Print the last lines of a log with tail -n and find the newest log with ls -lt.
You met these commands in Terminal-Start, lesson 5. Here they meet real logs.
Short files: cat
cat prints everything at once. For a hostname or a small config that is exactly right:
cat /etc/hostname
Purpose: Prints a whole file. Good for short files such as configs.
Syntax: cat FILE...
Example:
cat /etc/hostname
web-01
cat /etc/app/app.conf
# web-01
port = 8080
workers = 2
log_level = info
log_dir = /var/log/appLong files: less
less opens a file full-screen and waits. The arrow keys move line by line, the space bar a page at a time. /WORD and
Enter search forward, n jumps to the next match, G to the end, and q takes you out again. The block shows the start
of what you would see; in the terminal, run less logs/app.log to get the real full-screen view.
less logs/app.log
Purpose: Lets you page and search through long files. q ends it.
Syntax: less FILE
Options:
Example:
less logs/app.log | head -n 3
09:12:04 INFO worker 1 started
09:12:04 INFO worker 2 started
09:13:40 INFO request served in 91msThe end of a file: tail
New lines go to the end of a log, so the end is what you want. tail shows the last 10 lines, head the first 10, and
-n picks another number:
tail -n 3 logs/app.log
Purpose: tail prints the end of a file, head the start. -n sets how many lines.
Syntax: tail [-n COUNT] FILE
Options:
Example:
tail -n 3 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
head -n 2 logs/app.log
09:12:04 INFO worker 1 started
09:12:04 INFO worker 2 startedWhich log, though? Do not guess. ls -lt puts the file written last on top, and head cuts the list down:
ls -lt /var/log/app | head -n 3
total 12
-rw-r--r-- 1 app app 45 Oct 17 09:41 worker.log
-rw-r--r-- 1 app app 621 Oct 17 09:41 app.log
Watching a log live
On a real server tail -f logs/app.log stays open and prints every new line the moment it arrives, and Ctrl-C ends the
watching without touching the log. It is the right tool during a deploy. less +F does the same and lets you scroll back
after Ctrl-C. This simulator does not follow files yet, so here you work with tail -n.
Practice
Terminal task 1/2
Mission
Print exactly the last 3 lines of logs/app.log.
Task 1/2: Print exactly the last 3 lines of logs/app.log.
Mission
Print exactly the last 3 lines of logs/app.log.
Task complete
Solution
tail -n 3 logs/app.log Why: The last lines tell you what happened most recently. tail works on any file without counting. sed and awk, which come later in this path, can print the same lines, but only if you tell them where they are.
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/2
A service just restarted and you want to see how the log starts. Print the first 2 lines of logs/app.log.
Task 2/2: A service just restarted and you want to see how the log starts. Print the first 2 lines of logs/app.log.
A service just restarted and you want to see how the log starts. Print the first 2 lines of logs/app.log.
Task complete
Solution
head -n 2 logs/app.log Why: The start of a log often shows the version and settings a service came up with. That is where a wrong config shows first.
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.
Build the command
Build the line that cuts the list of /var/log/app, sorted by time, down to its first 3 lines.
Your command
Building blocks
Check works once your command has at least one block.
Find the error
This line is supposed to follow a growing log on a real server. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Put in order
A service misbehaves and you only know the log folder. Put the steps in a sensible order.
Steps
- Search for details in less with /
- Find the newest log with ls -lt
- Read its last lines with tail -n
- Change to the log folder with cd
Next up: what you do when you have forgotten how a command works.
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: cat /etc/hostname, less logs/app.log, tail -n 3 logs/app.log
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: done
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- 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
-
tail -n 50 <file>the last 50 lines -
head -n 5 <file>the first 5 lines -
less <file>page and search, q ends it -
ls -lt | head -n 3top of the list by time, the first line is the total
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, 19 KB)
- View as image (PNG, 55 KB)