Getting a portion of text from input files in Linux is a common operation. 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 in which 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
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. 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 10 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
-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 3 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 to, the
head command can also print the file content by byte. Just pass the
-c option to the command. Keep in mind that newline count as a single character, so if
head prints out a newline, it will count it 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 3 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 be piped to other commands. 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
ls -t /etc | head -n 5
ld.so.cache resolv.conf systemd libreoffice profile.d
By now you should have a good understanding of how to use the Linux
head command. Now, let’s take a look at the
Tail Command in Linux
Tail command in Linux is same as the
head command. Unlike the
head command, however, the
tail command prints the last few number of lines (10 lines by default) of a certain file.
The basic syntax of tail command is:
tail [OPTIONS] FILES
For example, the following command will print the last 10 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 also 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 . We can also pipe the output from other commands into
For example, to identify the five files or folders in
/etc directory with the oldest modification times, and pipe the output into
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 powerfull feature in
tail command. Sometimes the input file we want to check is changing. For example, a running application may append its output to a log file. If we execute the
tail command with the
-f option on the changing file, all newly added lines will be appended to standard out. This might be by far the most useful and commonly used option for
For example, you can see new lines that are added to the end of a 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 commands print 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 have to use the both commands together.
Let’s say we want to get from the 5th to the 10th line from the
/etc/passwd file. At first,
head command will retrieve first 10 lines and
tail command will retrieve the last 5 line from the output of
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
That’s all for now. In this article we’ve learned some typical usages of both commands through examples. As you can see, both the
tail and the
head commands are very useful for controlling exactly what file content will print to the screen. Certainly, they are flexible commands that can improve the management of your files greatly. Give them a try.