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 man Command: Read Command Manual Pages

Linux man Command: Read Command Manual Pages

Learn how to use the Linux man command to find and read command manuals, sections, options, and related pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux fsck Command: Check and Repair Filesystems

Linux fsck Command: Check and Repair Filesystems

Learn how to check and Repair Filesystems with the Linux fsck command, including practical examples, key options, and important precautions.

How to Check the Linux Distribution, Version, Kernel, and Architecture

How to Check the Linux Distribution, Version, Kernel, and Architecture

Learn how to identify a Linux distribution and version with /etc/os-release, then check the running kernel and CPU architecture with hostnamectl and uname. The guide also covers scripts, legacy systems, containers, WSL, and chroot environments.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux shred Command: Overwrite File Data and Understand Its Limits

Linux shred Command: Overwrite File Data and Understand Its Limits

Learn how Linux shred overwrites file data, which options control its passes and removal, and why SSDs, snapshots, backups, and modern filesystems limit its guarantees.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.

Bash Command Operators Explained: ;, &&, ||, &, and |

Bash Command Operators Explained: ;, &&, ||, &, and |

Understand how Bash operators run commands sequentially, conditionally, in the background, or through pipelines, with practical examples and precedence guidance.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.