Home
Last updated
The head
command is a command-line utility for outputting the first part of files given to it via standard input. It writes results to standard output. By default head
returns the first ten lines of each file that it is given.
To view the first ten lines of a file pass the name of a file to the head
command. The first ten lines of the file will be printed to standard output.
head /usr/share/dict/words
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
To set the number of lines to show with head
pass the -n
option followed by the number of lines to show.
head -n 1 /usr/share/dict/words
A
To limit the number of bytes shown with head
pass the -c
option. Instead of limiting by number of lines this will limit by the number of bytes passed to the -c
option. In the following example the output is limited to 16 bytes.
head -c 16 /usr/share/dict/words
A
a
AA
AAA
Aache%
To show the first ten lines of multiple files pass more than one filename to the head
command. This will output the first ten lines of each file to standard output with a header indicating which file is being shown.
head /usr/share/dict/words /usr/share/dict/french
==> /usr/share/dict/words <==
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
==> /usr/share/dict/french <==
ça
AAAI
abaissé
abaissa
abaissai
abaissaient
abaissais
abaissait
abaissant
abaissas
To suppress the header line pass the -q
option. This can be useful to combine files.
head -q /usr/share/dict/words /usr/share/dict/french
A
a
AA
AAA
Aachen
aah
Aaliyah
Aaliyah's
aardvark
aardvark's
ça
AAAI
abaissé
abaissa
abaissai
abaissaient
abaissais
abaissait
abaissant
abaissas
The head
command can be piped to from other commands. In the following example the output of the ls
command is piped to head
to only show the five most recently modified files or folders.
ls -t /etc | head -n 5
ld.so.cache
ssh
pam.d
shadow
passwd
Have an update or suggestion for this article? You can edit it here and send me a pull request.