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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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
- 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
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.
- 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
-noption 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
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
-noption or simply specifying a number after the
-sign in your command.
- 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
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.
- 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
-qoption. 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
-qoption, the head command omits the filenames, and displays the x number of lines of each file together.
$ head -q cars.txt states.txt
-qoption, the head command was able to output the last 10 lines of both our
states.txtfile 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.
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
- In this example we will display a specified number of bytes of data from a text file. For example, let’s use our
cars.txtfile to display the first 50 bytes of the data in the file.
$ head -c 50 cars.txt
As you can see in the screenshot above, the first 7 car brands in our
cars.txtfile make up exactly 50 bytes of data in the file.
- Use the
-voption to display data from a specific file with the name of the file preceding the data.
$ head -v cars.txt
With this option, as you can see in the screenshot above, the
-voption for the head command printed the first 10 lines of the
cars.txtfile with the filename displayed before it.
- 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
sortcommand with the
-roption can be used with head command if you add a pipe.
$ head cars.txt | sort -r
In the screenshot above, you can see that the head command printed the first 10 lines of our
cars.txtfile and the
sortcommand with the
-roption sorted those lines in the reverse order.
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.