Let’s learn about the head command in Linux with options and examples. head command is mainly used to view the first ten lines of the file.
By using this command you can also see the content with the limited lines, limited Sizes, and can view the multiples files.
Q. How to display the 10 lines using the head command in Linux?
So, when we use the head command it will print only the upper 10 lines of a file.
So by default, the file “head_example” contains 20 lines, but we have to print only a few lines.
Command: # head head_example
We can see that it has printed the first 10 lines, but you can also use a pipe option to check the few lines.
Command: # cat head_example | head
Here we have used a pipe(|) option and then head command.
Q. How to display N number of Lines using the head command?
So by default it prints the first 10 lines what if we have to print 12 lines?
Command: # head -n 15 head_example
Here I have used -n option to print the number of lines as I want, also you can use without the n option the result will be same.
Command: # head -15 head_example
Q. How to display the filename as a Header in Output.
To display the name of a file you can use the option -v, which used to print to print headers.
Command: # head -v head_example
Q. How to display Multiple files using the head command.
So now I have two files with the name “check_the_number_of_line” and “head_example”
Command: # head head_example check_the_number_of_line
So, with the header it has given us the output, but what if we don’t want to print the file name in the place of header.
For that, we will use a -q option to never print the filename. Let’s see the example.
Command: # head -q head_example check_the_number_of_line
Q. How to display the content by Size.
So, let’s use -c option to check the content by the size of the file. By default, the size is in bytes.
So, the file check_the_number_of_file has 166 bytes and we will check the first 100 bytes of the file.
Command: # head -c check_the_number_of_line
What if we have to check the content till 5 KB? Let’s construct the command.
Command: # head -c5k check_the_number_of_line
Cheat Sheet of head command
Thanks to read the POST keep Supporting us!