Today we are going to discuss another handy text utility that is part of the GNU Core Utilities package. The head command allows you to display the beginning of a file. In this short tutorial we will discuss how to use the head command, it's options, and show some real world examples.

Using the head Command

The most basic why to use head is to simply pass a file as an argument. By default head will display the first 10 lines of the file.

[savona@putor ]$ head breathe.txt 
Breathe, breathe in the air 
Don't be afraid to care
Leave but don't leave me
Look around, choose your own ground
For long you live and high you fly
And smiles you'll give and tears you'll cry
And all your touch and all you see
Is all your life will ever be
Run, rabbit run
Dig that hole, forget the sun

NOTE: You can pass multiple file names.

Print X Number of Lines From File

You can print x number of lines by using the -n option followed by the desired number. Here we will print the first 3 lines of the file.

[savona@putor ]$ head -n 3 breathe.txt 
Breathe, breathe in the air
Don't be afraid to care
Leave but don't leave me

If you pass a negative number to the -n option, it will print ALL but the x number of lines. Here we will print all but the last 3 lines of the file.

[savona@putor ]$ head -n -3 breathe.txt 
Breathe, breathe in the air
Don't be afraid to care
Leave but don't leave me
Look around, choose your own ground
For long you live and high you fly
And smiles you'll give and tears you'll cry
And all your touch and all you see
Is all your life will ever be
Run, rabbit run
Dig that hole, forget the sun
And when at last the work is done
Don't sit down, it's time to dig another one
For long you live and high you fly

Print X Number of Characters from File

This also works with bytes (characters) when using the -c option. Each character in ASCII character set (English) is a byte. Therefore, you can use this option to show x number of characters from the file. Here we will use the head command to display the first 12 characters in our file.

[savona@putor ]$ head -c 12 breathe.txt
Breathe, bre[savona@putor TMP]$

NOTE: The head command does not add a newline or carriage return after printing the characters. This causes your prompt to be on the same line as the output.

You can also pass a negative number to print all but x number of bytes. Here we are printing all but the last 200 characters. Remember, spaces and other special characters are included in the count.

[savona@putor ]$ head -c -200 breathe.txt
Breathe, breathe in the air
Don't be afraid to care
Leave but don't leave me
Look around, choose your own ground
For long you live and high you fly
And smiles you'll give and tears you'll cry
And all your touch and all you see
Is all your life will ever be
Run, rabbit run
Dig that hole, forget the sun
And when [savona@putor TMP]$ 

Print Header Showing File Name

You can use the -v for a verbose output that will show the file name as the first line of output.

[savona@putor TMP]$ head -v cities.txt 
==> cities.txt <==
Philadelphia
Camden
Pittsburgh
Harrisburg

The head command will print the headers by default when passing multiple file names as an argument.

[savona@putor TMP]$ head cities.txt menu.txt breathe.txt 
==> cities.txt <==
Philadelphia
Camden
Pittsburgh
Harrisburg

==> menu.txt <==
Pasta
Chicken
Brussel Sprouts
Corn
Diet Soda

==> breathe.txt <==
Breathe, breathe in the air
Don't be afraid to care
Leave but don't leave me

Never Print Header Showing File Name

To suppress the headers you can use the -q option. This comes in handy when you are passing multiple files to the head command and prefer NOT to see the headers.

[savona@putor TMP]$ head -q cities.txt menu.txt
Philadelphia
Camden
Pittsburgh
Harrisburg

Pasta
Chicken
Brussel Sprouts
Corn
Diet Soda

Additional Options of the Head Command

In addition to the options above, there are several other options that are rarely used.

The -z option allows you to set the line delimiter to NUL instead of newline.

Next is the --version option will output the version information for your head command installation.

Last but not least, the --help option will display the options available to you in your version of the head command.

Conclusion

The Linux head command is one of the more widely used of the GNU Core Utilities. It is a perfect compliment to the tail command, which allows you to display the last lines in a file.