5 Practical Examples of Head Command in Linux
Commands

5 Practical Examples of Head Command in Linux

Community
Community

You may know that the cat command is used to print the contents of a file, onto the terminal. The cat command prints the entire file onto the terminal.

Head is another way to view text file in Linux. You can use head command to print a specified number of lines from the beginning of the file.

Here’s the syntax of the head command:

head [option] [filename]

7 examples of head command

Let’s learn how to use the head command in Linux with practical examples.

I’ll use the file agatha.txt in this example and here is the content of this text file. You can download the file to practice the commands while following this tutorial:

The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

If you don’t use any options with the head command, it will print the first 10 lines by default

head agatha.txt 
 The Mysterious Affair at Styles
 The Secret Adversary
 The Murder on the Links
 The Man in the Brown Suit
 The Secret of Chimneys
 The Murder of Roger Ackroyd
 The Big Four
 The Mystery of the Blue Train
 The Seven Dials Mystery
 The Murder at the Vicarage

If the file has less than ten lines, it will print all the lines, of course.

1. Print top N lines with head command

When you are in need to print a specific number of lines, you can use -n option followed by the number of lines.

For example, to display the first 3 lines, you can use this:

head -n 3 agatha.txt 
 The Mysterious Affair at Styles
 The Secret Adversary
 The Murder on the Links

2. Print all except last N lines

You can exclude a specific number of lines at the end of the file and print the remaining content of the file by providing a negative number to -n option.

For example, if you want to leave the last 15 lines of the file, you can use this command:

head -n -15 agatha.txt 
 The Mysterious Affair at Styles
 The Secret Adversary
 The Murder on the Links
 The Man in the Brown Suit
 The Secret of Chimneys

3. Using multiple files with head command

You can provide more than one file as input to the head command.

head -n N file1 file2 file3

For example, if you have to display the first two lines of two files, you can use something like this:

head -n 2 agatha.txt sherlock.txt 
 ==> agatha.txt <==
 The Mysterious Affair at Styles
 The Secret Adversary
 ==> sherlock.txt <==
 A Scandal in Bohemia
 The Red-Headed League

As you can see, the output for each file is separated with ==> filename <==.

4. Dealing with header in the output

As you saw in the last example, the head command prints the file name as the header above the output of each file to separate them.

You can use the -q option (quiet mode) to omit the file name from the output.

head -q -n 2 agatha.txt sherlock.txt 
 The Mysterious Affair at Styles
 The Secret Adversary
 A Scandal in Bohemia
 The Red-Headed League

You might have also noted that the header is not printed for a single input file. You can force it to print the file name with -v option (verbose mode).

head -v -n 2 agatha.txt 
 ==> agatha.txt <==
 The Mysterious Affair at Styles
 The Secret Adversary

Note – The size of one character is one byte.

5. Printing specific number of bytes/characters

If you require to print a specific number of bytes of a file, you can use -c option followed by the number.

Normally, the size of one character is one byte. So you can think of it as printing certain number of characters.

head -c3 agatha.txt 
The

You can also exclude a specific number of bytes at the end as you excluded a specific number of lines at the end. To do that, specify a negative value to -c option.

head -c -50 agatha.txt

Bonus Tip: Print N number of lines of a file by combining head and tail commands

What if you want to print N number of lines in the middle of a file?

For example, if you want to print the lines from 10 to 15 of a file, you can combine the head command with the tail command.

head -n 15 agatha.txt | tail -n +10

The head command prints the first 15 lines of the file. Then the tail command takes this output and prints all the lines starting from line number 10. This gives you the lines from 10 to 15.

If you just want to print the nth line, you can do it by combining head and tail again.

head -n 15 agatha.txt | tail -n 1

So, the head command prints the first 15 lines of the file and then the tail command prints the last line of this output. Thus, you get the 15th line.

I hope that you’ve understood the usage of head command and its options. If you have any query, please comment below!



Join the conversation.