$
Learn Linux handbook
en
XP:0
Quest 14Peeker
+180 XP

Peeking at Files (head & tail)

Launch Interactive Terminal for This Quest→No installation required • Runs fully in browser

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.

Let's inspect the logs. Use 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 / SyntaxDescription
tail -n 2 logs.txtDisplay last 2 lines of logs.txt
tail -f logs.txtMonitor logs.txt updates in real-time
head -n 5 logs/app.logDisplay first 5 lines of app.log
tail -n 3 logs/security.logDisplay 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.