Quick take: Use head -n N file to see the first N lines and tail -n N file for the last N. The standout feature is tail -f, which follows a growing log file live as new lines are written.

Introduction

The head and tail commands show the beginning and end of a file — the perfect tools when you do not need the whole thing. head is ideal for previewing a file's structure, while tail is the go-to for logs, where the newest entries are at the bottom.

This guide covers showing a set number of lines, following live logs with tail -f, and combining both commands with pipes to slice output.

Syntax

The basic syntax of the head and tail command is:

head [OPTIONS] [FILE...]
tail [OPTIONS] [FILE...]

Common Options and Parameters

The most useful options and parameters for the head and tail command:

OptionDescription
-n NShow N lines (head: first N; tail: last N).
-n +Ntail: start from line N to the end.
-c NShow N bytes instead of lines.
-ftail: follow the file and print new lines as they are added.
-Ftail: follow even if the file is rotated/recreated.
-qQuiet — do not print file name headers for multiple files.

Practical Examples

Real head and tail commands you can run today:

# First 10 lines (the default)
head report.csv
# First 5 lines
head -n 5 report.csv
# Last 20 lines of a log
tail -n 20 /var/log/syslog
# Follow a log live
tail -f /var/log/nginx/access.log
# Follow across log rotation
tail -F /var/log/app.log
# Skip the header row, show the rest
tail -n +2 data.csv
# Show lines 10 to 20 with a pipe
head -n 20 file | tail -n 11

Tips and Best Practices

  • tail -f is the fastest way to watch a log while reproducing a bug; press Ctrl+C to stop.
  • Use tail -F (capital F) for logs that rotate, so following continues on the new file.
  • tail -n +2 is a neat trick to skip a CSV header before further processing.

Final Thoughts

head and tail let you peek at files without loading them whole — head for the top, tail for the bottom. The live-follow mode tail -f alone makes tail indispensable for anyone working with logs. Pair them with grep and pipes to zero in on exactly the lines you need.

FAQ: head and tail Command in Linux

How do I see the first lines of a file?+

Use head: head file shows the first 10 lines, and head -n 5 file shows the first 5. Add -c N to show bytes instead of lines.

How do I see the last lines of a file?+

Use tail: tail file shows the last 10 lines and tail -n 50 file shows the last 50 — ideal for checking the most recent log entries.

How do I follow a log file in real time?+

Use tail -f /path/to/log. It prints new lines as they are written. Use tail -F to keep following even when the log is rotated.

How do I skip the header of a CSV file?+

Use tail -n +2 file.csv, which starts output at line 2 and prints everything after the header row.

How do I show a specific range of lines?+

Combine head and tail: head -n 20 file | tail -n 11 prints lines 10 through 20. For more complex ranges, sed -n '10,20p' file also works.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support