terminal.cab

Linux Basics Lesson 2 of 38

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

Reading logs without drowning

9 minutes

Learning goals

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/app

in the glossary: cat

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

  • /WORD search forward, n jumps to the next match
  • G jump to the end of the file
  • q quit

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

in the glossary: less

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

  • -n 50 the last 50 lines instead of 10
  • -f on a real server, keep following new lines; not in this simulator yet

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 started

in the glossary: tail

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

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

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

Esc, then Tab leaves the terminal

Not done yet

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

    Tap the blocks below to build the 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

    1. Search for details in less with /
    2. Find the newest log with ls -lt
    3. Read its last lines with tail -n
    4. Change to the log folder with cd

    Next up: what you do when you have forgotten how a command works.

    Quiz

    1. Question 1 of 3An 8 GB log, and you want to look around in it and search. What do you use?

      Check works once you have chosen an answer.

    2. Question 2 of 3You opened a file with less. How do you get out?

      Check works once you have chosen an answer.

    3. Question 3 of 3How many lines does tail print without any option?

      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 3, Help and history (7 min)

    What you can do now: cat /etc/hostname, less logs/app.log, tail -n 3 logs/app.log

    +0 XP in this lesson

    1. Lesson 1: open
    2. Lesson 2: done
    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: open
    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 3, Help and history (7 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.