← All commands
THE COMMAND REFERENCE

head

Print the first lines or bytes of files.

Last updated

GNU

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 GNU head

GNU head is part of GNU coreutils, and this page's checks ran coreutils 9.11. It accepts a negative count such as -n -2, which means everything except the last two lines.

GNU examples

Print everything except the last two lines. A negative count is a GNU extension:

head -n -2 notes.txt

References

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