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 scriptingIntroduction 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 system | Package name | Installation |
---|---|---|
AlmaLinux | coreutils |
|
Arch Linux | coreutils |
|
Debian | coreutils |
|
Fedora | coreutils |
|
Red Hat Enterprise Linux | coreutils |
|
Rocky Linux | coreutils |
|
openSUSE | coreutils |
|
Ubuntu | coreutils |
|
Your Linux distribution using a different package? Share your feedback.
Usage
Available options
Long option | Short option | Description |
---|---|---|
--bytes=[-]NUMBER | -c | Show 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 | -n | Show 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 | -q | Be quiet, which does not print headers, only data |
--verbose | -v | Verbose output, which always will print headers |
--zero-terminated | -z | Do 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.