terminal.cab

Terminal start Lesson 5 of 12

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

Looking into files: cat, less, head and tail

10 minutes

Learning goals

The whole file: cat

cat prints a file completely. For short files that is all you need:

cat notes.txt

Purpose: Prints the whole content of a file.

Syntax: cat [OPTION]... FILE...

Options:

  • -n put a line number in front of every line

Example:

cat notes.txt
Buy milk
Call the dentist
Water the plants

in the glossary: cat

With -n, every line gets a number in front. Handy when someone says "look at line 3":

cat -n notes.txt
     1	Buy milk
     2	Call the dentist
     3	Water the plants

cat only reads. The file stays exactly as it was.

Long files: less

For a file with 50 lines, cat rushes everything past and you only see the end. less opens the file on its own screen instead, one page at a time:

less Documents/recipes.txt

Purpose: Shows a file one screen at a time, with scrolling and search.

Syntax: less FILE

Example:

less Documents/recipes.txt

in the glossary: less

Inside less, these keys help:

When you quit, the screen of less disappears and the terminal looks as before, which is why the example above shows no output. The file was only read, never changed.

Start and end: head and tail

head shows the first lines of a file, tail the last ones. Without -n you get 10 lines. Logs grow at the bottom, so the newest entries are always at the end:

head -n 3 projects/website/server.log

Purpose: Shows the first lines of a file, 10 if you do not say otherwise.

Syntax: head [-n NUMBER] FILE

Options:

  • -n how many lines to show

Example:

head -n 3 projects/website/server.log
08:00 server started
08:01 GET /index.html 200
08:02 GET /style.css 200

in the glossary: head

tail -n 3 projects/website/server.log

Purpose: Shows the last lines of a file, 10 if you do not say otherwise.

Syntax: tail [-n NUMBER] FILE

Options:

  • -n how many lines to show

Example:

tail -n 3 projects/website/server.log
08:27 GET /favicon.ico 404
08:29 GET /index.html 200
08:30 server stopped

in the glossary: tail

So the server stopped at 08:30. That is the first clue to the answer.

Practice

Terminal task 1/2

Show the last 5 lines of the log projects/website/server.log.

Task 1/2: Show the last 5 lines of the log projects/website/server.log.

Show the last 5 lines of the log projects/website/server.log.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Terminal task 2/2

Show only the first 2 lines of the letter Documents/letter.txt.

Task 2/2: Show only the first 2 lines of the letter Documents/letter.txt.

Show only the first 2 lines of the letter Documents/letter.txt.

Loading terminal …

lerner@terminal: ~bash
lerner@terminal:~$

Esc, then Tab leaves the terminal

Not done yet

Fill the gap

You are reading a file in less. To quit and get back to the prompt, you press the key .

You are reading a file in less. To quit and get back to the prompt, you press the key ….

Check works once you have typed an answer.

Put in order

You want to find the first recipe with garlic in Documents/recipes.txt and then get back to the prompt. Put the steps in order.

Steps

  1. Press q to quit.
  2. Press n to jump to the next hit.
  3. Type less Documents/recipes.txt and press Enter.
  4. Type /garlic and press Enter.

Predict the output

Command

tail -n 1 notes.txt

Before the command, in your home folder ~

  • ~/notes.txt
What does tail -n 1 notes.txt print?

Check works once you have chosen an answer.

Try less Documents/recipes.txt in the terminal above: leaf through with Space, search for /garlic, quit with q. Next lesson: copying, moving and renaming.

Quiz

  1. Question 1 of 4How many lines does head show if you give no -n?

    Check works once you have chosen an answer.

  2. Question 2 of 4When is less the better choice than cat?

    Check works once you have chosen an answer.

  3. Question 3 of 4You searched for /garlic in less. How do you get to the next hit?

    Check works once you have chosen an answer.

  4. Question 4 of 4Which statements are true? Choose TWO.

    Check works when exactly 2 are chosen. Chosen: 0.

Cards

  1. Card 1 of 4

    Tap the card or press Space to turn it over.

  2. Card 2 of 4

    Tap the card or press Space to turn it over.

  3. Card 3 of 4

    Tap the card or press Space to turn it over.

  4. Card 4 of 4

    Tap the card or press Space to turn it over.

End of the lesson

Still open: 5 exercises, 4 quiz questions

Next: lesson 6, Copying, moving and renaming (10 min)

What you can do now: cat notes.txt, less Documents/recipes.txt, head -n 3 projects/website/server.log, tail -n 3 projects/website/server.log

+0 XP in this lesson

  1. Lesson 1: open
  2. Lesson 2: open
  3. Lesson 3: open
  4. Lesson 4: open
  5. Lesson 5: done
  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
1 of 12

Next: lesson 6, Copying, moving and renaming (10 min) That's enough for today

Path complete

You finished all 12 lessons of Terminal start.

On to Linux Basics: lesson 1, Finding your way around a strange server (8 min) Back to the path

Cheat sheet