head command in Linux with options

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
Q. How to display the 10 lines using the head command in Linux?

Sytnax:
# head [file_name]

Example:
# head head_example
Q. How to display N number of Lines using the head command?

Sytnax:
# head -n number [file_name]

Example:
# head -n 12 head_example
# head -12 head_example
** This will show first 12 lines of the head_example file **
Q. How to display the filename as a Header in Output?

Sytnax:
# head -v [file_name]

Example:
# head -v head_example
Q. How to display Multiple files using the head command?

Sytnax:
# head [file_name1] [file_name2] [file_namen]
# head -q [file_name1] [file_name2] [file_namen]
**-q option to never print the file name in the place of Header**

Example:
# head first_file second_file third_file
# head -q first_file second_file third_file
Q. How to display the content by Size?

Sytnax:
# head -c [size] [file_name]

Example:
# head -c 100 check_the_number_of_line
# head -c5k check_the_number_of_line

[wp_quiz id=”2906″]

Refer the link for less command and more command to see the logs.

Thanks to read the POST keep Supporting us!

Give your valuable time