How to Use the Linux Head Command

File handling is a day-to-day task of Linux users and administrators. One of the most commonly performed file-handling tasks is accessing the file’s content. This becomes more interesting and complicated when the files contain gigantic data with hundreds and thousands of lines and we need only a few lines from the start. In that case, we can use the Linux head command that fetches the file’s first N lines. This command helps us keep things simple by fetching only the necessary data instead of loading the complete file’s content.

In this tutorial, we will explain the head command and how to use it in Linux to display a file’s content from the top.

What is Linux Head Command

In Linux, the commands like cat, head, or tail, are used to display the content of a file. The cat command displays the entire file while the tail command shows the content from the end. However, the head command reads the content from the beginning of the file. By default, it displays the top 10 lines of the selected file.

Basic Syntax

To use the head command in Linux, specify the head keyword followed by the name of the target file. Additionally, we can specify optional arguments to modify the behavior of the Linux head command according to our requirements:

head [options] fileName

Common Options

We can run the head command with one of the following optional arguments to modify the output:

  • -n: Specifies the number of lines to display from the beginning of the file.
  • -c: Defines the number of bytes to display from the start of the file.
  • -v: Displays the file name in the output when processing multiple files.
  • -q: Hides the file name when showing the content of multiple files.

Practical Examples of Head Command

Let’s go through the following head command examples to see how it works with or without optional flags. We’ve already created a sample file named sampleFile.txt, and we will use it in all the examples to demonstrate the head command:

cat sampleFile.txt
sample file

Example 1: Basic Usage of the head Command

Let’s run the head command without any optional argument to get the top ten lines of the sampleFile.txt:

head sampleFile.txt
head command without option

Example 2: Using the head Command With -n Option

In the following code, we use the -n option with the head command to show the specific number of lines in the output:

head -n 7 sampleFile.txt

This time the head command returns the top seven lines of the sampleFile.txt:

head command with -n option

Example 3: Using the head Command With -c Option

You can execute the head command with the -c option to specify the number of bytes in the output:

head -c 14 sampleFile.txt

The output shows the first 14 bytes of the sampleFile.txt, where bytes include characters, spaces, and newlines:

head command with -c option

Example 4: Using the head Command With -v Option

Let’s run the head command with the -v or –verbose option to print N number of lines along with the file name. This option is specifically useful when dealing with multiple files:

head -n 3 -v sampleFile.txt

This command will print the first three lines of the smapleFile.txt along with the file name:

head command with -v option

Example 5: Showing Top N Lines of Multiple Files

We can use the head command with a space-separated syntax to show the top N lines of multiple files:

head -n 3 sampleFile.txt exampleFile.txt

The output shows that the above command prints the top three lines of both specified files:

show top n lines of multiple files

From the output, you can observe that each file’s content is separated by the respective file name.

Example 6: Using the head Command With -q Option

You can use the -q option to print the multiple files’ content without specifying their names in the output:

head -n 3 -q sampleFile.txt exampleFile.txt

In the following output, the first three lines are from sampleFile.txt, while the other three lines are from exampleFile.txt:

head command with -q option

Example 7: Redirecting the Output of the head Command to a File

We can use the > sign to redirect the output of the head command to a file instead of printing it on the console:

head -n 5 sampleFile.txt > sampleClone.txt

We can verify the output by using the cat command, as follows:

cat sampleClone.txt
display content of multiple files

Example 8: Using the head Command With Pipe Operator

We can pipe the output of the head command to one or more commands to modify the output. For example, in the following command, we use the head command with the ls command to display the first few entries of a directory listing:

ls -l | head -n 6

This command will show the first six files and directories in the long format (-l):

head command with pipe operator

This is how the head command works in Linux.

Conclusion

The head command is an essential utility for Linux users and administrators. It allows them to access the file’s content from the start. Moreover, it simplifies file handling by fetching only the necessary lines or bytes, especially when dealing with large files. It can be used with several options to tailor the output according to our needs. In this article, we discussed how to use the head command in different scenarios, including working with multiple files and redirecting output.

The head command enables you to quickly view the top N lines of files. This streamlines your file-handling tasks. To enhance your experience and boost performance, consider Ultahost’s fast VPS hosting. With a range of affordable VPS plans, you can easily find one that suits your needs.

FAQ

What is the head command in Linux?
What options can I use with the head command?
Can I change number of lines displayed by the head command?
How can I display the first few bytes of a file using the head command?
How can I show the top N lines of multiple files?
Is it possible to redirect the output of the head command to a new file?
Can I use the head command in combination with other commands?

Related Post

How to Extract .tar.gz Files in Linux

The "tar.gz" file format is a common archive format use...

Explore the man (Manual) Command in Linux

The man command, short for “manual”, is an essentia...

How to Use the dmesg Linux Command

The dmesg command is a useful tool in Linux that lets y...

How to Set Environment Variables in Linux

Environment variables play an important role in shaping...

Setting up BitrixVM Environment on CentOS

BitrixVM is a powerful and flexible virtual machine des...

How to Create a Bash Script: Step-by-Step Gui

Bash scripts are powerful tools that allow you to autom...

Leave a Comment