Linux head Command Options with Examples - Display First Part of Files

February 14, 2014 | By
| Reply More

In Linux system, we have some alternatives about how to display a content of text files. We can use cat command, more command or less command. Another command to do it is head command.

What is head command

Head is command that will print only the first part of a text file. So when you know you need only the first part of the file, head will help you. By default, it will display the first 10 lines.

How to use head command

To use head command is quite easy. We just need to type :

$ head file_name

For example, we have a file with 15 lines inside it.

Original file

When we run head command, then the result will be like the screenshot below.

Head command

This output will be the same if we use -q option.

Display based on line

We already know that head by default will print the first 10 lines. But we can override this setting using -n option followed by the number of line. For example, if we want to display the first 5 lines, then the command will be like this :

$ head -n 5 doc_10.txt


$ head -5 doc_10.txt

Display based on lines

Display based on file size

We can also view the content based on file size. For example, the doc_10.txt file has a size of 447 bytes. If we want to know the content of the first 100 bytes, we can use -c option followed by the size. Here’s an example.

Display by size

Please notice that the result may not look neat, since head will cut the result based on size.
-n option may have a multiplier suffix :

  • b =512
  • kB = 1000
  • K = 1024
  • MB = 1000 * 1000
  • M = 1024 * 1024
  • GB = 1000 * 1000 * 1000
  • G = 1024 * 1024 * 1024

Display the file name as header

You may need this information if you copy the result into another file. Here’s a sample of the header of a file named doc_10.txt

$ head -v doc_10.txt

Print the file name

Display multiple files

To display multiple files, you can put the files separated by space. Here’s a sample.

$ head -7 doc_10.txt doc_30.txt

View multiple files


This command is also one of the basic command in Linux system. Once again, if you are only need the first part of the file, head may the right tool for you. As usual, you can alway type man head or head --help to display its manual page to explore more detail.


Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.