In this comprehensive guide, we will learn to use the head command in Linux. Moreover, we will also get familiar with the options for the head command that is available on Linux. The head command is available in all major Linux distributions; but for demonstration purposes, in this guide, we will only use Ubuntu 20.04 LTS. Let’s begin with understanding the syntax of the head command first:

Syntax:

The syntax of the head command is like any other command that is used to work with files. It takes two different parameters; OPTION and FILE_NAME. The options are used to manipulate the output of the head command. They can be used to specify the amount of data that needs to be printed to the standard output or to print data with or without headers. Here’s a list of options available for the head command in Linux: In this how-to guide, we will discuss the head command’s options in great detail; but first, we will learn to use the head command without any options.

How to show first 10 lines from a file

If we execute the head command without providing any option, it will show the first ten lines only of a text file. Let’s say we have a text file named cars.txt which contains the names of some cars and we need to print the names of the first ten cars which are written on the first ten lines. Then we will use the head command in the following way:

How to print a specific number of bytes from the beginning of a file

The options -c or –byte can be used to specify the number of bytes that are to be printed to the standard output. Now we will use the cars.txt file again and print the first fifteen bytes of the file: Similarly:

How to print a specific number of lines from the beginning of a file

The -n, –lines options can be used to print a specific number of lines from the beginning of a file: Similarly:

How to display the header/file name

The -v or –verbose option can be used to print the name of the file to the standard output:

How to display data from multiple files

The head command can take multiple file names as parameters. It will print out the first ten lines of both files (proceeded by the file name) by default: We can also use options along with multiple file names:

How to display data from multiple files without headers

The -q, –quiet and the –silent option can be used to display data from multiple files without headers:

How to combine head command with other commands

The head command can be combined with other commands to filter the data that is being printed to the standard output:

Using [-]K with -c and -n options

If the -c and -n options are followed by [-]K, then the head command prints all the bytes/lines of the file except the last K bytes/lines: The names.txt file contains twenty-six names in total. If we use the below-given command then the head command will print the first fifteen names (lines). But if we add a - before the number then the head command will print all the lines except the last fifteen:

Conclusion

The head is a command present in all major Linux distributions which are used to print out data from the start of a file. It is the opposite of the tail command which is used to output data from the end of a file. The head command can be used with different options to specify the number of lines or bytes that should be printed to the standard output. Options are also available to allow or restrict the head command to not show the header or name of the file. In this how-to guide, we learned to use the header command along with its options.