How to Use the Linux Command head and 4 Essential Options

The Linux Command head is a useful tool for viewing the top portion of text files. In this post, we’ll provide a detailed explanation of the head command, starting from basic usage to various options.

What is the Linux Command head?

The head command is used to display the first part of a text file, typically the top few lines. It is particularly useful for quickly checking log files or configuration files. By default, the head command outputs the first 10 lines of a text file, but you can specify the number of lines to display as needed.

Basic Usage of the head Command

Let’s start with the basic usage of the head command. The simplest way to use it is as follows:

head filename
ShellScript

When you enter the command above, it will output the first 10 lines of the file specified by filename. For example, if you want to check the top 10 lines of a file called example.txt, you would use:

head example.txt
ShellScript

This command will display the first 10 lines of example.txt.

Figure 1. Linux Command head: Output example
Figure 1. Linux Command head: Output example

Options for the head Command

In addition to its basic functionality, the head command provides several options for viewing file contents in various ways. Let’s explore the main options one by one.

-n (Specify Number of Lines)

By default, the head command outputs the first 10 lines. However, you can use the -n option to specify the exact number of lines you want to display.

head -n 5 example.txt
ShellScript

This command will display the first 5 lines of example.txt. You can enter the desired number of lines after the -n option.

Figure 2. Linux Command head: Specifying the number of lines to display with the -n option
Figure 2. Linux Command head: Specifying the number of lines to display with the -n option

-c (Specify Number of Bytes)

If you want to view the first few bytes of a file instead of lines, you can use the -c option.

head -c 30 example.txt
ShellScript

This command outputs the first 30 bytes of example.txt. This option is especially useful when dealing with large files or files that contain data other than plain text.

Figure 3. Linux Command head: Specifying the number of bytes to display with the -c option
Figure 3. Linux Command head: Specifying the number of bytes to display with the -c option

-q (Suppress Header Output)

When checking multiple files at once, the file name headers are displayed along with the content, which can enhance readability. However, if you prefer to see only the file contents without the headers, you can use the -q option.

head -q file1.txt file2.txt
ShellScript

This command outputs the first 10 lines of both file1.txt and file2.txt, but omits the file name headers. The figure below shows the first 3 lines of a.txt and b.txt, where the headers are displayed when the -q option is not used, and omitted when the -q option is applied.

Figure 4. Linux Command head: Suppressing file name headers with the -q option
Figure 4. Linux Command head: Suppressing file name headers with the -q option

-v (Force Header Output)

Conversely, if you want to display the file name header even when only one file is being checked or when the header is normally omitted, you can use the -v option.

head -v example.py
ShellScript

This command displays the first 10 lines of example.py along with the file name as a header. The figure below shows the file name example.py being displayed as the header.

Figure 5. Linux Command head: Forcing file name header display with the -v option
Figure 5. Linux Command head: Forcing file name header display with the -v option

Examples of Using the head Command

The head command is extremely useful when monitoring log files or quickly checking the basic information of configuration files. For example, when analyzing server log files, you might want to inspect the initial state rather than the latest entries, which can be done by viewing the first few lines using the head command.

Additionally, if you need to check multiple text files simultaneously, you can use the -q option to output the contents of each file consecutively, making it easier to compare the contents.

Cautions

When using the head command, it’s important to remember that the larger the file, the more challenging it becomes to view the entire content. Therefore, you should carefully set the number of lines or bytes you need to inspect. Moreover, when dealing with binary files or files in special formats, unexpected output may occur, so proceed with caution.

Summary

The Linux head command is a powerful tool for quickly and easily checking the top portion of a text file. By utilizing various options such as -n, -c, -q, and -v, you can make the command even more useful, whether you’re analyzing log files, checking configuration files, or performing other tasks. Mastering this command can significantly enhance your efficiency when working in a Linux environment.

References

Leave a Comment