Linux Head Command Explained for Beginners (5 Examples)

When you work on the command line of Linux, you sometimes want to have a quick look at the first lines of a file. For example, if a log file is constantly being updated, you may want to see the first 10 lines of the log file each time. Although it is always an option to view the file in an editor (e.g. vim), there is a command line tool called head that allows you to easily view the first lines of a file.

In this article, we will explain the basics of the head command with some easy-to-understand examples. Please note that all steps/instructions mentioned here have been tested on Ubuntu 22.04.

Linux head command

As mentioned in the beginning, the head command lets users view the first part of files. Here's its syntax:

head [OPTION]... [FILE]...

And following is how the command's man page describes it:

Print the  first  10 lines of each FILE to standard output. With more than one FILE, precede each 
with a header giving the file name.

The following Q&A-type examples should give you a better idea of how the tool works:

Q1. How to print the first 10 lines of a file on terminal (stdout)?

This is quite easy using head - in fact, it's the tool's default behavior.

head [file-name]

The following screenshot shows the command in action:

Using the Linux head command

Q2. How to tweak the number of lines head prints?

While 10 is the default number of lines the head command prints, you can change this number as per your requirement. The -n command line option lets you do that.

head -n [N] [File-name]

For example, if you want to only print first 5 lines, you can convey this to the tool in the following way:

head -n 5 file1.txt

Show first lines of a file on Linux

Q3. How to restrict the output to a certain number of bytes?

Not only number of lines, you can also restrict the head command output to a specific number of bytes. This can be done using the -c command line option.

head -c [N] [File-name]

For example, if you want head to only display first 25 bytes, here's how you can execute it:

head -c 25 file1.txt

Display the first bytes of a file

So you can see that the tool displayed only the first 25 bytes in the output.

Please note that [N] "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, and so on for  T,  P, E, Z, Y."

Q4. How to have head print filename in the output?

If, for some reason, you want the head command also to print the file name in the output. You can do that using the -v command line option.

head -v [file-name]

Here's an example:

Show filename in output of head command

So as you can see, the filename 'file 1' was displayed in the output.

Q5. How to have NUL as line delimiter, instead of newline?

By default, the head command output is delimited by newline. But there's also an option of using NUL as the delimiter. The option -z or --zero-terminated lets you do this.

head -z [file-name]


As most of you'd agree, head is a simple command to understand and use, meaning there's little learning curve associated with it. The features (in terms of command line options) it offers are also limited, and we've covered almost all of them. So give these options a try, and when you're done, take a look at the command's man page to know more.

Share this page:

0 Comment(s)

Add comment

Please register in our forum first to comment.