The Linux head command reads and prints the first N lines to standard output. By default, it prints out the first ten lines of a file to standard output. However, this can be modified by passing additional arguments on the command-line. The ‘head’ command is the opposite of the tail command that prints out the last N lines of a given file. In this guide, we focus on the Linux head command and feature a few use cases of the command.
Syntax:
The head command takes the following Syntax:
$ head [options] files(s)
1) Display the first ten lines of a file
As discussed in the introduction, the head command – without any arguments – displays first ten lines of a file. In the example below, we have a sample text file – asian_countries.txt – that contains a list of countries in the Asian continent.
To list the first 10 countries in the file, run the command:
$ head asian_countries.txt
2) Display the file name tag
Using the -v flag, you can display the filename tag before printing out the lines in the file as follows:
$ head -v asian_countries.txt
3) Display output from multiple files
Additionally, you can pass multiple files in a single command as shown below. This time, the name of each text file is printed first before the lines with the output of the first file preceding the next file.
In the example below, we have 2 text files: asian_countries.txt and europe_countries.txt. The output of the asian_countries.txt file is first printed out, followed by the europe_countries.txt file. Notice there’s a filename tag that comes before the lines.
$ head asian_countries.txt europe_countries.txt
Above head command will display first ten lines of each file.
4) Display the first N number of lines
You can define the lines you want to display using the -n flag followed by the number of lines you want to print. For example, to print the first 5 lines, run the command:
$ head -n 5 asian_countries.txt
5) Redirect output to a text file
Instead of printing to standard out, you can save the output of the head command to a text file or a log file using the redirection operator (>). If the file does not exist, it is created, and the output is stored. Take note that this overwrites everything in the file.
The command below saves the output of the first 4 lines in the asian_countries text file to the output.txt file
$ head -n 4 asian_countries.txt > output.txt
To prevent the file from being overwritten, use the double greater than operator (>>) to append the output to the file.
In the command below, we are appending the output of the first 4 lines of the europe_countries text file to the output.txt file without overwriting it.
$ head -n 4 europe_countries.txt >> output.txt
Using the cat command, observe how the file contains the output of countries in Asia as well as those in Europe.
6) Use the head command with pipes
You can pipe commands to head to print N number of lines. For example, you can print the first 15 entries in the /etc directory as shown.
$ ls /etc | head -n 15
You can also pipe the output of the head command to other commands such as the sort command for better formatting.
$ head -n 5 asian_countries.txt | sort > output2.txt
7) Check the version of the head command
To check the version of the head command, invoke the command below.
$ head --version
8) Get additional options
To get help with the command usage, visit the man pages as shown.
$ man head
Thanks for coming this far. We hope that this tutorial shed light and eliminated any confusion on the usage of the Linux head command.
Also Read : 9 tee Command Examples in Linux