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
tail commands to print only the lines we are interested in.
Table of Contents
- Head Command in Linux
- Tail Command in Linux
- How to Use head and tail Commands Together in Linux
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] FILES
By default, without any option, the
head command will display the first ten lines from the file. Just like this.
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/nologin
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
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/nologin
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
head -c 8 /etc/passwd
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
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:
-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:
How to Use head Command with Pipes
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
ls -t /etc | head -n 5
ld.so.cache resolv.conf systemd libreoffice profile.d
By now, you should understand how to use the Linux
head command well. Now, let’s take a look at the
Tail Command in Linux
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] FILES
For example, the following command will print the last ten lines from the
#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-1
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-1
How to Use tail Command with Pipes
Earlier, we piped the output from
ls. Similarly, we can also pipe the output from other commands into
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.conf
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
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.log
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"
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
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 -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/nologin
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.