View the Beginning of Text Files with head

Traducciones al Español
Estamos 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.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

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
Jacob

To 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
bmp

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.