Peeking at Files (head & tail)
Concept & Overview
Now that you can read a whole file with cat, what if the file has thousands of lines and you only want to see the top or bottom? Dumping a huge file with cat floods your screen. Instead, we use head and tail to peek at the edges.
- The head command displays the first 10 lines of a file by default. You can change this using -n (e.g. head -n 5 logs/app.log for the first 5 lines).
- The tail command displays the last 10 lines by default, supporting the same -n option (e.g. tail -n 2 logs.txt to view the last 2 lines of a file).
- Crucially, tail -f logs.txt (follow) monitors a file in real-time, showing new lines as they are written by the system. This is a sysadmin's favorite tool for watching live logs.
tail -n 2 logs.txt (or head -n 5 logs/app.log / tail -n 3 logs/security.log) to view file lines, and run tail -f logs.txt to simulate real-time log monitoring (it will print mock entries and exit).Command Syntax Reference
| Command / Syntax | Description |
|---|---|
tail -n 2 logs.txt | Display last 2 lines of logs.txt |
tail -f logs.txt | Monitor logs.txt updates in real-time |
head -n 5 logs/app.log | Display first 5 lines of app.log |
tail -n 3 logs/security.log | Display last 3 lines of security.log |
Verification Objectives
These conditions will be automatically verified when you run commands inside the interactive terminal simulator:
- ○Run tail with -n (e.g. tail -n 2 logs.txt)
- ○Run tail with -f (e.g. tail -f logs.txt)
Ready to complete Quest 14?
Test your skills live in the browser terminal with an emulated Linux filesystem, real-time command evaluation, and instant step-by-step hints.