head command in Linux with examples

The head command in Linux is one of the most important commands you’ll need when viewing text files. If you’re new to Linux, the head command is a perfect place to start, since it has a simple syntax and straight forward purpose. The head command is the complementary command to the tail command. The head command is used to print the first 10 lines (by default) of one or multiple files.

In this article, we’ll show you some basic ways to use the head command through easy examples and in-depth explanations. The head command works very similar to the tail command. If you’re at all familiar with this command, learning how to use the head command should be fairly easy.

In this tutorial you will learn:

  • How to use the head command on Linux

"<yoastmark

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software head
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The head command prints the first x number of lines in a specific file. By default, it will display the first 10 lines of the specified file. The purposes of the options you can use with the head command are displaying contents in a file in different ways depending on the situation. These options will provide specific information that you’ll find convenient and often more useful than playing around with a GUI text editor.

head command in Linux Basic Examples

  1. Running the head command without any additional options will print the last 10 lines of a file you specify in the command.
    $ head cars.txt
    
    Show its first 10 lines
    Show its first 10 lines

    As you can see in the screenshot above, the output will list a few lines per file. This output will always be the same; the same 10 lines at the beginning – like the name of the command suggests – of a file if you run the head command without any additional options. You can change this result though, by using one of the various options with head. We’ll cover that in the next examples.

  2. Note that despite this file containing a list of 20 different car brands, simply running the head command without any extra options prints the first 10 brands present in the file. Using the -n option shown below, you can get the head command to churn out an output that’ll display a specific number (let’s say 15) of lines in a file.
    $ head -n 15 cars.txt
    OR
    $ head -15 cars.txt
    
    Using head to view the first 15 lines of a file
    Using head to view the first 15 lines of a file

    As you can see in the screenshot above, the output from head shows the first 15 lines of our file. This can be done with the -n option or simply specifying a number after the - sign in your command.

  3. In the examples above, we’ve shown how the head command can be used to print the first 10 lines of a file. But as you’ll recall from the introduction of this post, the head command can also display lines from more than one file at a time. We can achieve this by typing more than one file following the head command.
    $ head cars.txt states.txt
    
    Using head to display the first 10 lines of multiple files
    Using head to display the first 10 lines of multiple files

    As you can see in the screenshot above, printing the first 10 lines of data from multiple files can be achieved by typing each filename after the command.

  4. Now that you know how to use the head command to display the first 10 lines of data from multiple files, we can start learning how to use the -q option. By default, using the head command for multiple files at once will cause the command to display the filenames preceding the x number of lines of the specified files. If we use the -q option, the head command omits the filenames, and displays the x number of lines of each file together.
    $ head -q cars.txt states.txt
    
    Listing the first 10 lines of multiple files at once with the -q option
    Listing the first 10 lines of multiple files at once with the -q option

    Using the -q option, the head command was able to output the last 10 lines of both our cars.txt file and states.txt file and combine them without separating them by filename. It displays the first 10 lines by default, as we explained in an earlier section of this article.

Advanced Usage

The head command is pretty simple, but as you’ve observed throughout the examples section of this article, it comes packed with a lot of options. Many of these options fly under the radar, and even some seasoned system administrators may not know them. However, they can come in handy in various situations. In this section of the tutorial, we’ll show you a few of the lesser-known options of the head command that we think are useful.

head command in Linux Advanced Examples

  1. In this example we will display a specified number of bytes of data from a text file. For example, let’s use our cars.txt file to display the first 50 bytes of the data in the file.
    $ head -c 50 cars.txt 
    
    Display the first 50 bytes of data in a file
    Display the first 50 bytes of data in a file

    As you can see in the screenshot above, the first 7 car brands in our cars.txt file make up exactly 50 bytes of data in the file.

  2. Use the -v option to display data from a specific file with the name of the file preceding the data.
    $ head -v cars.txt
    
    The -v option will show which file head is displaying data from
    The -v option will show which file head is displaying data from

    With this option, as you can see in the screenshot above, the -v option for the head command printed the first 10 lines of the cars.txt file with the filename displayed before it.

  3. The head command can be used with pipes |. This means that the head command can produce an output and another command can be used in conjunction with this output for various purposes. For example, the sort command with the -r option can be used with head command if you add a pipe.
    $ head cars.txt | sort -r
    

    "You

    In the screenshot above, you can see that the head command printed the first 10 lines of our cars.txt file and the sort command with the -r option sorted those lines in the reverse order.

Closing Thoughts

In this tutorial, we learned all about the head command on Linux which is important to master for personal use and system administration to view text files on the command line.