Blog about Web Programming and Electronics
💬 👤
back Back to blog
Linux

How the tail and head Commands Work: A Practical Step-by-Step Guide for Linux

When working in the Linux terminal, inspecting an entire file is often unnecessary when you only need a quick look at the first few lines or the most recent log entries. To optimize system efficiency and workflow speed, Linux provides two lightweight and essential utilities: head and tail.

Creating Sample Test Data in One Step

To follow along and immediately test both commands without requiring special permissions or system paths, generate a sample log.txt file containing 20 numbered lines directly in your terminal:

seq -f "Line %g: System event or log message" 1 20 > log.txt

What is the head Command?

The head command outputs the beginning of a file — defaulting to the first 10 lines. It provides an immediate overview of file headers or configuration entries without loading the entire file into memory.

head log.txt

head -n 4 log.txt

The first command outputs the default 10 lines, while the -n 4 flag restricts the output strictly to the first 4 lines.

What is the tail Command?

The tail command performs the inverse operation — it outputs the end of a file (defaulting to the last 10 lines). This is particularly useful for logs, where new diagnostic records are appended at the bottom.

tail log.txt

tail -n 4 log.txt

Real-Time Log Streaming: tail -f

The most versatile capability of tail is monitoring files live using the -f (follow) parameter. It prints the initial lines and remains attached, outputting any new lines added to the file in real time.

tail -f log.txt

You can test this by appending text in another terminal window using echo "New event" >> log.txt. Stop monitoring at any time by pressing Ctrl + C.

All-in-One Practical Demonstration

To verify the entire workflow in a single step with a clean terminal screen, run this unified command sequence:

seq -f "Line %g: System event or log message" 1 20 > log.txt && clear && echo "=== 1. FULL FILE (Total Lines: 20) ===" && wc -l log.txt && echo -e "\n=== 2. HEAD COMMAND (First 4 lines) ===" && head -n 4 log.txt && echo -e "\n=== 3. TAIL COMMAND (Last 4 lines) ===" && tail -n 4 log.txt

The structured output clearly demonstrates how head extracts lines 1–4 while tail extracts lines 17–20.

Command Reference Table

Command Operational Result
head log.txt Prints the default first 10 lines
head -n 4 log.txt Prints strictly the first 4 lines
tail log.txt Prints the default last 10 lines
tail -n 4 log.txt Prints strictly the last 4 lines
tail -f log.txt Streams newly appended lines in real time
head -c 20 log.txt Extracts the first 20 bytes of the file

Composing Workflows with Pipes (|)

Connecting commands via pipes (the | character) allows the output of one process to flow directly into another without temporary files:

ls -la | head -n 5

ps aux | tail -n 20

cat log.txt | grep "1" | head -n 5

The first pipeline displays the first 5 directory entries, the second shows the last 20 active processes, and the third filters for lines containing the number 1 before showing the first 5 results.

Once you are finished testing, clean up the temporary file with: rm log.txt.

back Back to blog