head Command Explained

head Command Explained

head shows you the beginning of a file. It sounds almost too simple to need explaining, but the number of situations where “just the first few lines, please” is exactly the right amount of information makes it one of the most quietly useful commands in daily Linux use.

Basic usage

head file.txt
# prints the first 10 lines by default

head access.log
# quickly check the format and earliest entries in a log file

Ten lines is the default, chosen as a reasonable amount to fit comfortably on a terminal screen without scrolling, while still giving enough context to understand a file’s structure.

Controlling how many lines to show

head -n 20 file.txt     # first 20 lines
head -n 1 file.txt        # just the very first line
head -20 file.txt         # shorthand form, same as -n 20

Any positive number works with -n, and there is a shorthand that drops the explicit flag name entirely on most systems, which is common enough in everyday use that both forms are worth recognizing.

Showing bytes instead of lines

head -c 100 file.bin
# first 100 bytes, regardless of how many lines that spans

head -c 16 mystery-file | xxd
# combine with xxd to inspect the raw magic bytes of a file,
# useful for identifying a file format manually

For binary files, where “lines” is not a meaningful concept, -c lets you grab a specific number of raw bytes instead. This is a common technique when manually verifying a file’s format by checking its magic bytes, the fixed sequence at the very start of many file types that identifies what kind of file it is.

head vs tail

head file.log     # first 10 lines: oldest entries in a typical log
tail file.log       # last 10 lines: newest entries in a typical log

Most log files append new entries to the bottom, which means head shows you the oldest recorded activity while tail shows you the most recent. Both tools exist because “give me a manageable slice of this file” is a need that comes up constantly, and which end of the file you actually want depends entirely on what you are looking for.

Viewing multiple files

head file1.txt file2.txt
# ==> file1.txt <==
# (first 10 lines of file1.txt)
#
# ==> file2.txt <==
# (first 10 lines of file2.txt)

When given multiple filenames, head labels each section with a header showing the filename, making it easy to quickly eyeball and compare the start of several files in a single command without opening each one separately.

# Suppress the filename headers even with multiple files
head -q file1.txt file2.txt

# Force headers even with a single file
head -v file.txt

head at the end of a pipeline

This is arguably where head earns its keep the most in daily use, trimming down output from another command that would otherwise be too much to usefully read:

# Ten most recently modified files, instead of the whole directory
ls -lt | head

# Top memory-consuming processes, instead of every process
ps aux --sort=-%mem | head

# First few matches from a broad search, as a quick sanity check
grep -r "TODO" . | head -20

Piping into head is a fast way to sanity-check a command before committing to viewing its full output, particularly useful when a command’s output volume is unpredictable and you just want a representative sample first.

Frequently Asked Questions

What does the head command do?

head prints the beginning of a file, ten lines by default. It is a quick way to peek at the structure or content of a file, such as checking a CSV’s header row or the first few log entries, without opening the entire file or scrolling through it in a pager.

How do I control how many lines head shows?

Use the -n flag followed by a number, such as head -n 20 file.txt to show the first 20 lines instead of the default 10. A shorthand without the -n also works on most systems: head -20 file.txt.

Can head show a specific number of bytes instead of lines?

Yes, the -c flag shows a specific number of bytes instead of lines, such as head -c 100 file.bin to see the first 100 bytes. This is useful for inspecting the beginning of binary files, where counting by lines does not make sense, such as checking a file’s magic bytes to identify its format.

How is head different from tail?

head shows the beginning of a file, while tail shows the end. They are complementary tools for the same basic need: viewing a portion of a file without opening the entire thing. head is typically used to check a file’s header, structure, or earliest entries, while tail is used to check the most recent entries, especially in log files where the newest information is appended at the bottom.

Can head display multiple files at once?

Yes, list multiple files as arguments, such as head file1.txt file2.txt, and head prints a labeled header line before each file’s output showing which file it belongs to, followed by the requested number of lines from that file. This makes it easy to quickly compare the beginnings of several files in one command.

Why is head useful when piped with other commands?

head is frequently used at the end of a pipeline to limit output that would otherwise be overwhelming, such as ls -lt | head to see only the ten most recently modified files instead of an entire directory listing, or ps aux —sort=-%mem | head to see only the top few memory-consuming processes rather than every running process on the system.