The head command is a command-line utility in most Linux distributions that is used to print data from the beginning of one or more files. It is also used to output piped data to CLI. We can use different options to print any number of lines or bytes of data to the standard output but by default the head command prints the first 10 lines of the file.
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:
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.
head [OPTION] [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:
|It is mandatory to follow this option by a number which specifies the bytes that are to be printed to the standard output.|
|It is mandatory to follow this option by a number as well. The -n option specifies the amount of lines that are to be printed to the standard output.|
|If this option is used then the head command will not print headers specifying the file names.|
|This option will always output the header/filename.|
|Changes the line delimiter from newline to NULL.|
|To output the help menu.|
|To output the version information about the head command.|
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
--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:
head -c 15 cars.txt
head --bytes 15 cars.txt
How to print a specific number of lines from the beginning of a file
--lines options can be used to print a specific number of lines from the beginning of a file:
head -n 5 cars.txt
head --lines 5 cars.txt
How to display the header/file name
--verbose option can be used to print the name of the file to the standard output:
head -v cars.txt
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:
head cars.txt names.txt
We can also use options along with multiple file names:
head -n 2 cars.txt names.txt
How to display data from multiple files without headers
--quiet and the
--silent option can be used to display data from multiple files without headers:
head -n 2 -q cars.txt names.txt
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:
ls | head -n 5
Using [-]K with -c and -n options
-n options are followed by [-]K, then the head command prints all the bytes/lines of the file except the last K bytes/lines:
head -n -15 names.txt
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).
head -n 15 names.txt command
But if we add a
- before the number then the head command will print all the lines except the last fifteen:
head -n -15 names.txt
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.