As the name shows, the head command displays the first N lines of data. By default, it is a 10 number but can be customized. It is opposite to the tail command because the tail command helps in displaying the last N lines from the file.
The Linux environment is necessary to run these commands on it. This will be done by having a virtual box and running an Ubuntu in it.
Linux provides the user information about the head command that will guide the new users.
$ head –help
Similarly, there is a head manual as well.
$ man head
To learn the concept of the head command, consider the file name data2.txt. The contents of this file will be displayed using the cat command.
$ cat data.txt
Now, apply the head command to get the output. You will see that the first 10 lines of the file’s content are displayed while others are deducted.
$ head data2.txt
The head command displays the first ten lines of the file. But if you want to get more or less than 10 lines, you can customize it by providing a number in the command. This example will explain it further.
Consider a file data1.txt
Now follow the under-mentioned command to apply on the file:
$ head –n 3 data1.txt
From the output, it is clear that the first 3 lines will be displayed in the output as we provide that number. The “-n” is mandatory in the command, otherwise,90l;…. it will show an error message.
Unlike the earlier examples, where whole words or lines are displayed in the output, the data is displayed corresponding to the bytes covered on the data. The first number of bytes is displayed from the specific line. In the case of a new line, it is considered as a character. So it will also be considered as a byte and will be counted so that the accurate output regarding bytes can be displayed.
Consider the same file data1.txt, and follow the below-mentioned command:
$ head –c 5 data1.txt
The output is describing the byte concept. As the number given is 5, the first 5 words of the first line are displayed.
In this example, we will discuss the method of displaying the content of more than one file by using a single command. We will show the usage of the “-q” keyword in the head command. This keyword implies the function of joining two or more files. N and the command “-“ is necessary to use. If we don’t use –q in the command and only mention two file names, then the result will be different.
Before Using –q
Now, consider two files data1.txt and data2.txt. We want to display the content present in both of them. As the head is used, the first 10 lines from each file will be displayed. If we don’t use “-q” in the head command, then you will see that the file names are also displayed with the file content.
$ Head data1.txt data3.txt
By Using -q
If we add the keyword “-q” in the same command discussed earlier in this example, then you will see that the file names of both files are removed.
$ head –q data1.txt data3.txt
The first 10 lines of each file are displayed in such a way that there is no line spacing between the content of both files. The first 10 lines are of data1.txt, and the next 10 lines are of data3.txt.
If you want to show the contents of a single file with the name of the file, we will use “-V” in our head command. This will show the filename and the first 10 lines of the file. Consider the data3.txt file shown in the above examples.
Now use the head command to display the file name:
$ head –v data3.txt
In this aforementioned article, we have discussed the basic to complex concept and functionality of the head command. Linux system provides the usage of the head in various ways.
You Can Learn More By Seeing The Manual Page of Head.