View the Beginning of Text Files with head
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
The head command is a core Linux utility used to view the very beginning of a text file. Despite its narrow functionality, head is useful in many systems administration and scripting tasks. For similar functionality that address the end of a file, use the
tail utility instead.


Using head
List the file or files you want to view after the head command:
head /etc/rc.conf
This will print the first 10 lines of /etc/rc.conf to standard output. If a file has fewer than 10 lines, head will print the entire file.
Control the Length of Output
With the -n option, the number of lines that head outputs can be modified:
head -n 24 /etc/logrotate.conf
This prints the first 24 lines of /etc/logrotate.conf to the terminal. You can specify the number of lines before or after you declare the file:
head /etc/logrotate.conf -n 24
If a file is smaller than the specified number of lines, head will print the entire file.
View Multiple Files
head can process multiple files at once:
head example.txt names.txt
==> example.txt <==
lollipop
The Joke
Jockey to the Fair
Simon's Fancy
Truckles
==> names.txt <==
John
Susan
Michael
Robert
Justin
Herbert
Marissa
George
JacobTo view the first line of every file in a directory, you can use the -n option combined with the * wild card:
head -n 1 *
View Command Output
By using the pipe operator, head can be used to filter the output of commands as well as files:
cat --help | head -n 2
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.ls /usr/lib | head
alsa-lib
ao
apr.exp
apr-util-1
aprutil.exp
aspell
aspell-0.60
avahi
awk
bmpThis page was originally published on
