Head and Tail Commands in Linux Explained with Examples

This guide shows the typical usages of head and tail command in Linux to get a portion of the text through examples.

Getting a portion of text from input files in Linux is common. However, sometimes, we are interested in viewing only a few lines of a file. Linux provides us the head and tail commands to print only the lines we are interested in.

Linux head and tail commands are very similar. They are, by default, installed in all Linux distributions. Let’s first understand what they are and what they are used for.

In short, as their names imply, the head command prints lines from the beginning of a file, and the tail command prints lines from the end of files. Finally, both commands write the result to standard output.

Now, let’s learn how to use them through examples.

Head Command in Linux

The syntax of the head command is pretty straightforward:

head [OPTIONS] FILESCode language: CSS (css)

By default, without any option, the head command will display the first ten lines from the file. Just like this.

head /etc/passwd
root:x:0:0::/root:/bin/bash
bin:x:1:1::/:/usr/bin/nologin
daemon:x:2:2::/:/usr/bin/nologin
mail:x:8:12::/var/spool/mail:/usr/bin/nologin
ftp:x:14:11::/srv/ftp:/usr/bin/nologin
http:x:33:33::/srv/http:/usr/bin/nologin
nobody:x:65534:65534:Nobody:/:/usr/bin/nologin
dbus:x:81:81:System Message Bus:/:/usr/bin/nologin
systemd-journal-remote:x:982:982:systemd Journal Remote:/:/usr/bin/nologin
systemd-network:x:981:981:systemd Network Management:/:/usr/bin/nologinCode language: JavaScript (javascript)

Of course, there are options that we can define while executing the command to get the customized output.

Output a Specific Number of Lines Using head Command

If you wish to retrieve a different number of lines than the default 10, then the -n option is used along with an integer telling the number of lines to be retrieved.

For example, the following command will display the first three lines from the /etc/passwd file.

head -n 3 /etc/passwd
root:x:0:0::/root:/bin/bash
bin:x:1:1::/:/usr/bin/nologin
daemon:x:2:2::/:/usr/bin/nologinCode language: JavaScript (javascript)

Output a Specific Number of Bytes Using head Command

In addition, the head command can also print the file content by byte. Just pass the -c option to the command. Keep in mind that the newline counts as a single character, so if head prints out a newline, it will count as a byte.

For example, the following command will display the first 8 bytes from the /etc/passwd file.

head -c 8 /etc/passwd
root:x:0Code language: CSS (css)

Output Multiple Files Using head Command

Of course, the head command can also handle multiple files. For example, the following command will show the first three lines of /etc/passwd and /etc/group files.

head -n 3 /etc/passwd /etc/group
==> /etc/passwd <==
root:x:0:0::/root:/bin/bash
bin:x:1:1::/:/usr/bin/nologin
daemon:x:2:2::/:/usr/bin/nologin

==> /etc/group <==
root:x:0:brltty,root
sys:x:3:bin
mem:x:8:Code language: JavaScript (javascript)

Adding the -q option to the example above will strip headers giving file names.

head -q -n 3 /etc/passwd /etc/group
root:x:0:0::/root:/bin/bash
bin:x:1:1::/:/usr/bin/nologin
daemon:x:2:2::/:/usr/bin/nologin
root:x:0:brltty,root
sys:x:3:bin
mem:x:8:Code language: JavaScript (javascript)

How to Use head Command with Pipes

The head command can also be piped to other commands. So, in the following example, the output of the ls command is piped to head to show the five most recently modified files or folders in the /etc directory.

ls -t /etc | head -n 5
ld.so.cache
resolv.conf
systemd
libreoffice
profile.dCode language: CSS (css)

By now, you should understand how to use the Linux head command well. Now, let’s take a look at the tail command.

Tail Command in Linux

The tail command in Linux is the same as the head command. However, unlike the head command, the tail command prints a specific file’s last few lines (10 lines by default).

The basic syntax of the tail command is:

tail [OPTIONS] FILESCode language: CSS (css)

For example, the following command will print the last ten lines from the /etc/locale.gen file.

tail /etc/locale.gen
#zh_HK.UTF-8 UTF-8
#zh_HK BIG5-HKSCS
#zh_SG.UTF-8 UTF-8
#zh_SG.GBK GBK
#zh_SG GB2312
#zh_TW.EUC-TW EUC-TW
#zh_TW.UTF-8 UTF-8
#zh_TW BIG5
#zu_ZA.UTF-8 UTF-8
#zu_ZA ISO-8859-1Code language: CSS (css)

Output a Specific Number of Lines Using tail Command

Similarly to the head command, you can print the last few lines using the -n option as shown below.

tail -n 3 /etc/locale.gen
#zh_TW BIG5
#zu_ZA.UTF-8 UTF-8
#zu_ZA ISO-8859-1Code language: CSS (css)

How to Use tail Command with Pipes

Earlier, we piped the output from head into ls. Similarly, we can also pipe the output from other commands into tail.

For example, to identify the five files or folders in the /etc directory with the oldest modification times and pipe the output to the tail command, type:

ls -t /etc/ | tail -n 5
wpa_supplicant
libpaper.d
papersize
mdadm.conf
gssapi_mech.confCode language: CSS (css)

Watch a File for Changes Using tail Command

There is one more powerful and widely used feature in the tail command. Here’s what it’s all about.

Sometimes the input file we want to check is changing. For example, a running application may append its output to a log file.

Therefore, if we execute the tail command with the -f option on the changing file, all newly added lines will be appended to standard output.

So, this might be the most practical and commonly used option for tail command.

For example, you can see new lines that are added to the end of an Nginx log file as they are added, like this:

tail -f /var/log/nginx/access.logCode language: JavaScript (javascript)
172.16.1.122 - - [08/Apr/2021:08:15:32 +0000] "POST /wp-admin/admin-ajax.php HTTP/1.1" 200 109 "https://linuxwizard.com/wp-admin/post.php?post=18254&action=edit" "Mozilla/5.0 (X11; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0" 
172.16.1.122 - - [08/Apr/2021:08:19:27 +0000] "GET /feed/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1" 
172.16.1.122 - - [08/Apr/2021:08:19:49 +0000] "HEAD /feed/ HTTP/1.1" 200 0 "-" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36" Code language: JavaScript (javascript)

As each new log entry is added to the log file, tail will update its display in the terminal window.

How to Use head and tail Commands Together in Linux

As the tail and head command prints different parts of files, we can combine these two to print some advanced filtering of file content.

For example, if you want to read the content from the middle of any file, you must use both commands together.

Let’s say we want to get from the 5th to the 10th line from the /etc/passwd file. At first, the head command will retrieve the first ten lines, and then the tail command will retrieve the last five lines from the output of the head command.

head -n 10 /etc/passwd | tail -n 5
http:x:33:33::/srv/http:/usr/bin/nologin
nobody:x:65534:65534:Nobody:/:/usr/bin/nologin
dbus:x:81:81:System Message Bus:/:/usr/bin/nologin
systemd-journal-remote:x:982:982:systemd Journal Remote:/:/usr/bin/nologin
systemd-network:x:981:981:systemd Network Management:/:/usr/bin/nologinCode language: JavaScript (javascript)

Conclusion

In this article, we’ve learned typical usages of both commands through examples. As you can see, both the tail and the head commands are handy for controlling what file content will print to the screen.

Indeed, they are flexible commands that significantly improve your files’ management. So, give them a try.

Need more details? Check the head, and the tail commands man pages.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

One comment

  1. This is a good article! It is really jam packed with helpful facts and it’s penned in an uncomplicated-to-fully grasp way. I Particularly value how the put up was backed up with investigate and serious-globe illustrations. Effectively carried out!

Leave a Reply

Your email address will not be published. Required fields are marked *