Linux for Developers Course
Linux for Developers
/
Beginner

head & tail

Definition

Commands used to output a specific number of lines from the beginning (`head`) or the end (`tail`) of a file. By default, they print 10 lines.

Explain Like I'm New

Snipping the top or bottom off a document. If you just want to see the most recent error at the very bottom of a log file, you use `tail`.

Real World Example

A server crashes. The developer types `tail -n 20 error.log` to see the very last 20 things that happened before the crash, completely ignoring the thousands of lines above it.

Common Use Cases

  • •Checking recent log entries
  • •Previewing CSV file headers

Crucial Command Flags

Flag / OptionDescription
-n [num]Output the first/last [num] lines (default is 10).
-c [bytes]Output the first/last [bytes] bytes.
-f (tail only)Follow. Output appended data as the file grows.

Interview Questions

basic

  • If you want to view the first 5 lines of a file, what command do you type?

intermediate

  • What does the highly useful `tail -f` command do?

Flash Cards

Question

First 5 lines?

Click to reveal answer
Answer

`head -n 5 filename.txt`

Question

What is tail -f?

Click to reveal answer
Answer

The 'follow' flag. It prints the bottom of the file, but keeps the terminal open. As new lines are written to the file (like a live server log), they are instantly printed to your screen in real-time.