head: show first number of lines from a file

Show a limited number of lines from the top of a file or standard input.

Typical usage: automation, data filtering, shell scripting

Introduction into head

The head command provides the first 10 lines of a file to the standard output. It can read multiple files or from the standard input, such as using pipes.

Installation

When head is not installed by default, it can be added to the system using the relevant software package.

Package information for head

Operating systemPackage nameInstallation
AlmaLinuxcoreutils
dnf install coreutils
Arch Linuxcoreutils
pacman -S coreutils
Debiancoreutils
apt install coreutils
Fedoracoreutils
dnf install coreutils
Red Hat Enterprise Linuxcoreutils
dnf install coreutils
Rocky Linuxcoreutils
dnf install coreutils
openSUSEcoreutils
zypper install coreutils
Ubuntucoreutils
apt install coreutils

Your Linux distribution using a different package? Share your feedback.

Usage

Available options

Long optionShort optionDescription
--bytes=[-]NUMBER-cShow the first number of bytes defined by NUMBER, with a leading minus (-) it will show all bytes except the LAST defined number of bytes
--lines=[-]NUMBER-nShow the first number of lines defined by NUMBER, with a leading minus (-), it will show all lines except the LAST defined number of lines
--quiet-qBe quiet, which does not print headers, only data
--verbose-vVerbose output, which always will print headers
--zero-terminated-zDo not use a newline (\n) but NUL (\0) as line delimiter

Missing an option in this overview? Share your feedback.

Examples using head

Show the first 5 entries from the passwd file

head --lines=5 /etc/passwd

Show a sequence of 1 up to 10, filter with head to show all lines, except the last 3

seq 1 10 | head --lines=-3

Example output when using a leading minus:

# seq 1 10 | head --lines=-3
1
2
3
4
5
6
7

Frequently Asked Questions

What is the head command and its purpose?

The head command is a command-line tool on Linux to filter a file by showing the first defined number of lines.

Which package provides the head command?

The command head is provided by the coreutils package.

Relevant articles using the head command

The following articles include an example on how to use head and might be worth further exploring.