Linux head Command: Display the Beginning of a File

The head command prints the beginning of a file or standard input. It defaults to the first ten lines and is useful for checking a header, sampling data, or limiting the visible portion of pipeline output.

This guide explains the basic syntax, key options, practical examples, related commands, common cautions, and frequently asked questions for head.

What Is the head Command?

Use -n to select a number of lines and -c to select bytes. With multiple files, a filename header normally identifies each result. GNU head also supports negative counts that mean all input except the final number of lines or bytes.

Basic Syntax

head [OPTION]... [FILE]...

Options and edge cases can vary by implementation and version. Check head --help and man head on the target system before relying on implementation-specific behavior.

Basic head Usage

Show the first ten lines

With no count option, head prints ten lines from each input.

head access.log

Choose a line count

Pass the required number to -n.

head -n 25 access.log

Choose a byte count

The count is bytes, not Unicode characters, so a multibyte character can be split.

head -c 64 sample.bin

Results of reading only the beginning

head -n 2 prints only the first two lines from the input. It is useful for checking small samples without changing the original.

printf 'one\ntwo\nthree\n' | head -n 2

Example output:

one
two

If the command before the pipe produces a lot of data, head may terminate early, causing a broken pipe error in the preceding command. In automation, be sure to check the exit status of the entire pipeline.

Key Options

Option Purpose
-n NUM Print the first NUM lines.
-c NUM Print the first NUM bytes.
-q Suppress filename headers for multiple inputs.
-v Always print filename headers.
-n -NUM On GNU systems, print all but the last NUM lines.
-c -NUM On GNU systems, print all but the last NUM bytes.

Practical Examples

Inspect a CSV header and sample

View the column header and a few records without opening the complete data set.

head -n 6 sales.csv

Compare beginnings of several logs

Force filename headers so the source of each section remains clear.

head -v -n 5 app.log worker.log

Limit a command result

The producer may receive SIGPIPE after head has enough input, which matters when interpreting a strict pipeline's status.

find /var/log -type f | head -n 20

head and Related Commands

Command Primary role Best fit
head Print the beginning Headers and samples
tail Print or follow the end Recent log entries
sed Select and transform ranges More complex line processing
less Browse the complete input Interactive search

Important Cautions

Byte counts are not character counts

A UTF-8 character can be cut between bytes and appear malformed.

Head preserves the existing order

Sort first if the required result is based on value, size, or time rather than current order.

Check pipeline exit semantics

With pipefail, an upstream SIGPIPE may produce a nonzero pipeline status even when head got the requested lines.

FAQ

How many lines does head print by default?

Ten lines.

How do I print only the first line?

Use head -n 1 FILE.

Which command prints the final lines?

Use tail.

Can I print the first 1 KiB?

GNU head accepts head -c 1K FILE; verify suffix support on other implementations.

Related Resources

More in This Category
Linux split Command: Divide Large Files into Smaller Parts

Linux split Command: Divide Large Files into Smaller Parts

Learn how to divide Large Files into Smaller Parts with the Linux split command, including practical examples, key options, and important precautions.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux Tutorial / Compare Files with diff and cmp

Linux Tutorial / Compare Files with diff and cmp

Use diff and cmp to compare two files, read unified diff output, and distinguish a detected difference from an actual command error.

Linux timedatectl Command: Check Time Zone and Time Synchronization

Linux timedatectl Command: Check Time Zone and Time Synchronization

Learn how to check Time Zone and Time Synchronization with the Linux timedatectl command, including practical examples, key options, and important precautions.

Essential Linux Commands: A Practical Reference by Task

Essential Linux Commands: A Practical Reference by Task

Browse essential Linux commands by task, including help, files, text processing, permissions, processes, networking, packages, storage, and system administration.

Linux cmp Command: Compare Files Byte by Byte

Linux cmp Command: Compare Files Byte by Byte

Learn how Linux cmp compares two files byte by byte, reports the first mismatch, runs silently in scripts, limits ranges, and differs from diff and checksums.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dirname Command: Extract the Directory Part of a Path

Linux dirname Command: Extract the Directory Part of a Path

Learn how to use the Linux dirname command to remove the final component of a path and return its directory portion, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux alias Command: Create Command Shortcuts

Linux alias Command: Create Command Shortcuts

Learn how to use the Linux alias command to create, inspect, and make shell command aliases persistent, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.