Master the Linux ‘head’ Command: A Comprehensive Guide

Peter Hou
3 min readMay 10, 2023

--

This guide provides a comprehensive introduction to the Linux head command, including its history, use cases, and various parameters. It's an essential command for viewing the initial part of files, making it especially useful when dealing with large files. The guide also covers common use cases and some advanced techniques that can be applied using the head command.

Instructions

This article aims to provide a complete understanding of the Linux head command. You will learn about its history, when and why to use it, how to use it, and its parameters. You will also explore common use cases and some tricky skills that could come in handy.

History

The head command is a part of the GNU core utilities package, which is available in almost all Unix-like operating systems. It was created for the purpose of outputting the initial part of files.

When and why to use it

The head command is used to display the beginning of a file. It is especially useful when working with large files where opening the entire file might be inefficient. It can also be used as part of a pipeline for more complex file processing tasks.

How to use it

You can use the head command by typing head followed by the filename in the terminal. By default, head will display the first 10 lines of the file.

head filename.txt

The commonly used parameters

  • -n This parameter lets you specify the number of lines from the beginning of the file that you want to display. For instance, head -n 5 filename.txt will display the first five lines of the file.
head -n 5 filename.txt
  • -c This parameter lets you specify the number of bytes from the beginning of the file that you want to display. For example, head -c 20 filename.txt will display the first 20 bytes of the file.
head -c 20 filename.txt

Other supported parameters

  • -q Use this parameter when you don't want head to print headers before each file output when multiple files are involved.
  • -v This parameter is the opposite of q. It forces head to print headers before each file output.

Most common use cases

One of the most common use cases of head is to check the initial contents of a file, particularly for large files. It is also commonly used in combination with other commands using pipes to perform more complex operations.

head filename.txt | grep "keyword"

The tricky skills

A neat trick is to use head in combination with tail to print specific lines or ranges of lines from a file. For example, head -n 20 filename.txt | tail -n 5 will print out lines 16 through 20 of the file.

head -n 20 filename.txt | tail -n 5

What needs to be noted

Remember that the head command deals with the beginning of files. If you're interested in the end of files, you should use the tail command instead.

Conclusion

The head command is a powerful tool that can increase your productivity when dealing with files in Linux. Understanding and mastering this command will certainly elevate your Linux skills.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.