How to Use ‘head’ Command in Linux [8 Useful Examples]

In Linux, there are various commands available to display the contents of the text file. Some of the popular and most frequently used commands are cat, less, more, view, etc. However, all of these commands are more relevant when we want to display a large part of the file.

Sometimes, we just want to display the first few lines of the file. In such, cases we can use the head command, which comes in handy when we want to display the first part of the file.

In this guide, we will learn about the head command using some practical examples. After following this guide, Linux users will be able to work with text files efficiently from the command line interface.

[ez-toc]

head Command Syntax

The syntax of the head command is very simple and it is identical to other Linux commands:

$ head [OPTIONS] [FILE-1] [FILE-2] ..

It is important to note that, in the above syntax, both OPTIONS and FILE parameters are optional. So, if the input file is not provided or the file argument is a hyphen (-) then, it reads the input from the stdin stream.

To start, first, let’s create a simple text file with the following contents:

$ cat file-1.txt
Create Sample Text File
Create Sample Text File

Now, the input file is ready. So let’s use it to demonstrate the usage for the head common.

1. Show First 10 Lines Of File in Linux

By default, the head command displays the first ten lines of the input file as shown.

$ head file-1.txt
Print First 10 Lines of File
Print the First 10 Lines of the File

Here, we can see that the command shows only the first ten lines of the file-1.txt file.

2. Show First N Lines of File in Linux

In the previous example, we saw that the head command displays the first ten lines of the file by default. However, we can overwrite this default behavior using the -n option, which allows us to limit the number of lines to be displayed.

To understand this, let’s use the below command to display the first five lines of the file-1.txt file:

$ head -n 5 file-1.txt
Print First N Lines of File
Print First N Lines of File

3. Remove Last N Lines of a File in Linux

In a similar way, we can use the negative number with the -n option to skip the last N lines from the file. For example, let’s use the -10 value to skip the last 10 lines of the file:

$ head -n -10 file-1.txt
Remove Last N Lines of File
Remove the Last N Lines of the File

In the above output, we can see that now the head command shows only the first two lines.

4. Show First N Characters of the File

We can also instruct the head command to display the first N bytes of the file using the -c option:

$ head -c 8 file-1.txt

In the below output, we can see that the head command shows the first eight characters from the file.

Print First N Characters of File
Print First N Characters of File

In this case, the file contains ASCII characters that take 1 byte per character. Hence the command shows the first eight characters including the newline (\n) character.

5. Remove Last N Characters of File

Similarly, we can use the negative number with the -c option to remove the last N bytes. So let’s skip the last line of the file-1.txt file using the below command:

$ head -c -9 file-1.txt

In the below output, we can see that the head command shows all the characters except the last nine characters.

Remove Last N Characters of File
Remove the Last N Characters of the File

6. Show File Name in Header of File

The head command allows us to display the current file name as a display header using the -v option:

$ head -n 5 -v file-1.txt

In the below output, ==> file-1.txt <== represents the display header.

Print File Name in Header of File
Print File Name in Header of File

This option comes in handy while working with multiple files. Hence the head command enables this option by default when we use multiple files with it.

7. Show File Name in Header in Multiple Files

We can use multiple files with the head command. In such cases, the display header is used to separate the file contents. Let’s understand this with a simple example.

First, let’s create a copy of the file-1.txt using the cp command:

$ cp file-1.txt file-2.txt

Now, let’s display the first three lines from each file:

$ head -n 3 file-1.txt file-2.txt
Show File Name in Header of Files
Show File Name in Header of Files

8. How to Disable the Display Header

In the previous example, we saw that by default, the head command enables the display header if we use multiple files with it. However, we can use the -q option to override this default behavior.

Let’s use the below command to display the first three lines from both files:

$ head -n 3 -q file-1.txt file-2.txt

Here, we can see that now the head command displays the file contents one after another without any display header.

Disable Header in Files
Disable Header in Files

In this article, we learned about the head command using practical examples. Linux newbies can refer to this guide while working with Linux systems.

Do you know of any other best example of the head command in Linux? Let us know your views in the comments below.

Two small things before you go

If something didn’t work on your setup, leave a comment and tell me which distro and version you’re on. I read every one, and most of these articles got better because a reader pointed out what was missing.

And if it did work, share it. TecMint has stayed free for 14 years because people pass these articles around, and that traffic is what pays for writing the next one.

Leave a comment
Get two free Linux courses

Learn the Linux basics with two free courses and join 34,000+ readers getting practical TecMint tutorials and selected Pro TecMint articles every week.

Pro TecMint
Get lifetime access for $99

If you want to go beyond the free tutorials, Pro gives you 18 courses, 5 ebooks, and every future course I add. One payment, lifetime access, no renewals.

Narendra K
Narendra K
Contributor
About the author

I'm an experienced and passionate software engineer with in-depth experience in Linux, Distributed systems, DevOps, and Cloud. My expertise lies in back-end web development, and the main languages in my tech stack are Java, Spring, Python, and Go. I’m a lifelong learner and an open-source enthusiast.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.