Linux head command for beginners (5 examples)

Sometimes, when working at the command line on Linux, you can quickly look at a few initial lines of a file. For example, if the log file is constantly being updated, you can look at, say, the first 10 lines of the log file each time. When viewing a file in an editor (like Vim) there is always an option, as the command line tool is head – makes it very easy to view the initial few lines of a file.

In this article, we’ll discuss the basics of the head command using some easy-to-understand examples. Please note that all steps / instructions mentioned here have been tested on Ubuntu 16.04LTS.

Linux head command

As mentioned at the beginning, the Head command allows users to view the first part of the files. Its syntax is:

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

And the following is how the man page describes the head command:

Печатает первые 10 строк каждого ФАЙЛА на стандартный вывод. С более чем один ФАЙЛ, перед каждым заголовком идет имя файла.

The following examples should give you a better understanding of how the tool works:

Q1. How do I print the first 10 lines of a file to the terminal (standard output)?

It’s pretty easy using head – in fact, that’s the tool’s default behavior.

head [file-name]

The following example shows the command in action:

[email protected]:/etc/nginx# head nginx.conf                                                                                                                     
user www-data;                                                                                                                                                
worker_processes 3;                                                                                                                                           

error_log  /var/log/nginx/error.log notice;                                                                                                                   
                                                                                                                                                              
worker_rlimit_nofile 65535;                                                                                                                                   

pid        /var/run/nginx.pid;                                                                                                                                

events {

Q2. How can I customize the output of a specific number of lines using the head command?

While 10 lines is the default, which the head command prints, you can change this number to suit your requirements. option -n the command line allows you to do this.

head -n [N] [File-name]

For example, if you only want to print the first 5 lines, you can pipe to this tool like this:

head -n 5 file1

[email protected]:/etc/nginx# head -n 5 nginx.conf                                                                                                                
user www-data;                                                                                                                                                
worker_processes 3;                                                                                                                                           
                                                                                                                                                              
error_log  /var/log/nginx/error.log notice;

Q3. How do I limit the output to a specific number of bytes?

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

head -c [N] [File-name]

For example, if you only want the first 25 bytes to be displayed, then this is how you can accomplish it:

head -c 25 file1

[email protected]:/etc/nginx# head -c 25 nginx.conf                                                                                                               
user www-data;

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

Please note that [N] ”May have a suffix multiplier: 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 do I print and the filename on the output?

If, for some reason, you want the head command to also print the filename on the output, you can do this with the option -v command line.

head -v [file-name]

Here’s an example:

[email protected]:/etc/nginx# head -c 25 nginx.conf                                                                                                               
user www-data;                                                                                                                                                
[email protected]:/etc/nginx# head -v nginx.conf                                                                                                        
==> nginx.conf <==                                                                                                                                            
user www-data;                                                                                                                                                
worker_processes 3;                                                                                                                                           
                                                                                                                                                              
error_log  /var/log/nginx/error.log notice;                                                                                                                   
                                                                                                                                                              
worker_rlimit_nofile 65535;                                                                                                                                   

pid        /var/run/nginx.pid;                                                                                                                                

events {

As you can see, the filename “file 1” was listed in the output.

Q5. How do I specify a NUL line separator instead of a newline?

By default, the output of the head command is delimited to a newline. But there is also the possibility of using NUL as a delimiter. Option -z or –Zero-terminated allows you to do this.

head -z [file-name]

Conclusion

As most of you will agree, the simple head command is straightforward and easy to use. The functions (in terms of command line parameters) that it offers are also limited, and we’ve covered almost all of them.

Sidebar