← All commands
THE COMMAND REFERENCE

head

Print the first lines or bytes of files.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

head prints the beginning of each file you name, ten lines by default. Use it to peek at a large file or to keep the first results of a pipeline.

Background

The head command appeared in PWB Unix, the Programmer's Workbench, according to the FreeBSD and macOS manuals. Today the name covers GNU head, the head that macOS ships, and the head applet in BusyBox. They agree on -n and -c with a positive count, and differ on negative counts and header options.

Examples

Print the first ten lines (all of a short file):

head notes.txt

Print the first two lines:

head -n 2 notes.txt

Print the first line of several files; each is introduced by a header:

head -n 1 app.log logs/server.log

Print the first five bytes:

head -c 5 notes.txt

Keep only the first two lines of a pipeline's output:

printf '1\n2\n3\n4\n' | head -n 2

Options

-n
Print this many lines instead of ten. The count is the next argument, as in -n 2.

-c
Print this many bytes instead of lines. A multibyte character can be cut in half.

Common mistakes

A count like -n 2 is a number of lines, not a line number. When several files are given, head prints a ==> name <== header for each; use -q where supported to hide it.

Portability

Only -n and -c with a positive count are in the Common examples. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox head

BusyBox bundles head as an applet that supports -n and -c. Other options depend on how it was built, and this page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox head supports -n and -c; other options depend on the build. A negative count is not part of the common examples.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.