Terminal start Lesson 5 of 12
Looking into files: cat, less, head and tail
10 minutes
Learning goals
- Print a short file with cat, with line numbers using cat -n.
- Read a long file page by page in less, search in it and quit with q.
- Show only the start or the end of a file with head and tail.
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:
Example:
cat notes.txt
Buy milk
Call the dentist
Water the plantsWith -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.txtInside less, these keys help:
- Space shows the next page,
bgoes back one page. /garlicand Enter searches for "garlic".njumps to the next hit,Nto the previous one.ggoes to the start,Gto the end.qquits and brings you back to the prompt.
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:
Example:
head -n 3 projects/website/server.log
08:00 server started
08:01 GET /index.html 200
08:02 GET /style.css 200tail -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:
Example:
tail -n 3 projects/website/server.log
08:27 GET /favicon.ico 404
08:29 GET /index.html 200
08:30 server stoppedSo 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.
Task complete
Solution
tail -n 5 projects/website/server.log Why: In a log, the newest entries are at the bottom. tail shows what happened last without scrolling through everything.
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
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.
Task complete
Solution
head -n 2 Documents/letter.txt Why: The first lines often tell you what a file is about, without reading all of it.
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.
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
- Press q to quit.
- Press n to jump to the next hit.
- Type less Documents/recipes.txt and press Enter.
- Type /garlic and press Enter.
Predict the output
Command
tail -n 1 notes.txtBefore the command, in your home folder ~
- ~/notes.txt
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
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works when exactly 2 are chosen. Chosen: 0.
Cards
-
Card 1 of 4
Tap the card or press Space to turn it over.
-
Card 2 of 4
Tap the card or press Space to turn it over.
-
Card 3 of 4
Tap the card or press Space to turn it over.
-
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
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
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: done
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: open
- Lesson 12: open
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
-
cat -n <file>print a file with line numbers -
less <file>read page by page, /word searches, q quits -
head -n 5 <file>the first 5 lines -
tail -n 5 <file>the last 5 lines