Linux head Command | Explained

In Linux, when the output of a file (on the terminal) exceeds tens of lines, then the head or the tail commands are used to view only the content from the head side or the bottom side. The head command prints only the first 10 lines from the head of the output file. This utility can be applied to a file, or it can also be piped with other commands.

This post will demonstrate the usage of the head command in Linux. The content for this post is shown below:

  • What is head Command in Linux?
  • How to Use the head Command in Linux?

What is head Command in Linux?

In Linux, the head command is a utility to print the first lines in one or multiple files. Users can specify the number of lines, but if they don’t, then by default, it will print the first 10 lines in the file. The syntax for the head command is shown below:

Syntax:

$ head [-options] [File Name]

Type the “head” keyword, and “options” along with the head command, and write the file name.

The most common options that can be used with the head command are shown in the below table:

OptionsPurpose
head -vDisplay the file tag name.
head -nFor the particular number of lines
head -cFor the particular number of bytes
head -qNever print headers giving the file name.

For more information and usages of the head command, you can use the help command in the terminal:

$ head --help

Let’s move forward and use this command in the Linux terminal.

How to Use the head Command in Linux?

There are various ways in which head command is used; some of the examples are given here. Consider the following file output while understanding the concept of all examples:

$ cat itslinuxfoss.txt

Example 1: Retrieving the Particular Number of Lines

To get the particular number of lines from the file, use the “n” option with the number. Check the following command:

$ head -n 5 itslinuxfoss.txt

The above command displayed the first 5 lines from the file as described in the command.

Example 2: Retrieving the Particular Number of Bytes

You can also use the head command to display the specified number of bytes using the “c” option. Each word, including the end of the line, is considered as 1 byte:

$ head -c 7 itslinuxfoss.txt

The word “London” has been displayed consisting of 6 words which are 6 bytes, and the 7th byte is the end of the line.

Example 3: Retrieving the File Tag Name

To display the file name as a tag, the user can use the “v” option to display it on the top of the output. To do so, run the given command in the terminal:

$ head -v itslinuxfoss.txt

The file name has been displayed as a tag name.

Example 4: Merging the two Files Output

This example will merge the output of the two files without displaying their tag name. For that purpose, the “q” option is used. Before using this option, check both files’ output without any option:

$ head itslinuxfoss1.txt itslinuxfoss2.txt

You can see that the file name is displayed for both files.

Now, let’s execute the same command but with the “q” option:

$ head -q itslinuxfoss1.txt itslinuxfoss2.txt

The content of both files has been displayed without displaying both file tag names.

Example 5: Saving the head Output to a File

Another example is to use the head command with the redirection “>” operator. Using this, the user is able to transfer the output of the head command to another file. To do this, the given command is used:

$ head itslinuxfoss.txt > Output.txt

After executing the above command, the output of the head command will be transferred to the “Output.txt” file.

Furthermore, run the “cat” utility to display the “Output.txt” file:

$ cat Output.txt

The “Output.txt” file has the head command output.

Example 6: Piping head Command With Other Commands

Another example is to use the head command with the pipe(|) command. The pipe command works like a chain and transfers the output of the first command which works as an input for the second command. Check the execution of the below command for further clarification:

$ cat itslinuxfoss.txt | head -5

The pipe command will execute the “cat” command for displaying the content of the file then this file output will be transferred to the “head” command as an input and the “head” command will display the first 5 lines.

Conclusion

In Linux, the head is a utility to display the first ten lines from the content of any file. The output of the head command depends upon the various options. This post has revealed the different usage of the head command in Linux. With the exception of this, displaying the file content using the awk command has also been exhibited in this write-up.