Skip to content

[UPDATE] View the Beginning of Text Files with head #1484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,57 @@
author:
name: Linode
email: [email protected]
description: Use the Linux command head to view the beginning of a text file
keywords: ["head", "linux", "common commands", "cli"]
description: 'Use the Linux command head to view the beginning of a text file'
og_description: 'Head is a Unix command line utility for viewing the beginning of text files. This guide shows how to use head and gives practical examples.'
keywords: ["head", "linux", "utilities", "cli", "text files"]
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
aliases: ['linux-tools/common-commands/head/']
modified: 2011-04-19
modified: 2018-02-02
modified_by:
name: Linode
published: 2010-10-25
title: View the Beginning of Text Files with head
---

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, consider the tail utility.
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](/docs/tools-reference/tools/view-and-follow-the-end-of-text-files-with-tail/) utility instead.

![Title graphic](/docs/assets/view_the_beginning_of_text_files_with_head_smg.png)
![View Beginning of Files with Head](/docs/assets/view_the_beginning_of_text_files_with_head_smg.png)

## Using head

Consider the following invocation:
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 the `/etc/rc.conf` file to standard output on the terminal. Thus, `head` is useful for a number of different situations such as determining the contents of a file if the file names are ambiguous.
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.

If a file has fewer than 10 lines, `head` will print the entire file.
### Control the Length of Output

### Control the Length of Output with head

With the `-n` option, the number of lines that `head` outputs can be modified. For example:
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 the `/etc/logrotate.conf` file to the terminal. You can specify the number of lines before or after you declare the file. Therefore, the following command is equivalent to the previous command:
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 the Beginning of Multiple Files with head
### View Multiple Files

`head` can process multiple files at once. Consider the following:
`head` can process multiple files at once:

{{< output >}}
$ ls
example roster
head example.txt names.txt

$ head *
==> example <==
{{< output >}}
==> example.txt <==
lollipop
The Joke
Jockey to the Fair
Simon's Fancy
Truckles

==> roster <==
==> names.txt <==
John
Susan
Michael
Expand All @@ -68,18 +65,24 @@ George
Jacob
{{< /output >}}

`head` outputs the first ten lines of each file by default. If you are using `head` to read more than one file, you may also use the `-n` option to control the number of lines printed.
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

### Combine head with Other Commands
By using the pipe operator, `head` can be used to filter the output of commands as well as files:

`head` can be used to filter the output of commands as well as files. For instance:
cat --help | head -n 2

{{< output >}}
% cat --help | head -n 2
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.
{{< /output >}}

ls /usr/lib | head

$ ls /usr/lib | head
{{< output >}}
alsa-lib
ao
apr.exp
Expand All @@ -91,5 +94,3 @@ avahi
awk
bmp
{{< /output >}}

In the first example, `head` filters the full output of `cat --help` to generate only the first two lines of the output of the command. In the second example, `head` prints the first ten lines of the output of the `ls` command.