Skip to main content

Sysadmin tools: Viewing text in Linux with tail and head

Explore the tail and head commands as new additions to your sysadmin toolbox for manipulating and reading text.
Image
Head and tail

Photo by Burst from Pexels

There are many commands to view the contents of files in Linux. Among those, the tail and head commands print the output of the last and first ten lines of a file, respectively. In some cases, the output or error of your submitted job or script creates a very big file and the most recent result prints at the end of the file. You can use tail, instead of the cat command, to just view the last few lines of the file. Sometimes you need multiple options with the tail command to view the contents of the file correctly. Below I’ll demonstrate the use of tail and head commands.

The synopsis of both commands is as follows:

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

I’ve created two greetings files in different languages with line numbers, as well. Each file has a total of 15 lines in it. Let’s see the contents of the files with a simple cat command:

root@jaibhim:~# cat greetings1.txt
1 Jai Bhim
2 Jai Bhim
3 Jai Bhim
4 Jai Bhim
5 Jai Bhim
6 Jai Bhim
7 Jai Bhim
8 Jai Bhim
9 Jai Bhim
10 Jai Bhim
11 Jai Bhim
12 Jai Bhim
13 Jai Bhim
14 Jai Bhim
15 Jai Bhim
root@jaibhim:~# cat greetings2.txt
1 Good Morning
2 Good Morning
3 Good Morning
4 Good Morning
5 Good Morning
6 Good Morning
7 Good Morning
8 Good Morning
9 Good Morning
10 Good Morning
11 Good Morning
12 Good Morning
13 Good Morning
14 Good Morning
15 Good Morning

Now I’ll use both the tail and head commands without any options to display the greetings1.txt file. The tail command gives me an output of the last ten lines, whereas the head command gives me the first ten lines.

root@jaibhim:~# tail greetings1.txt
6 Jai Bhim
7 Jai Bhim
8 Jai Bhim
9 Jai Bhim
10 Jai Bhim
11 Jai Bhim
12 Jai Bhim
13 Jai Bhim
14 Jai Bhim
15 Jai Bhim
root@jaibhim:~# head greetings1.txt
1 Jai Bhim
2 Jai Bhim
3 Jai Bhim
4 Jai Bhim
5 Jai Bhim
6 Jai Bhim
7 Jai Bhim
8 Jai Bhim
9 Jai Bhim
10 Jai Bhim

In the above example, you can see both commands print the first and last ten lines of the file.

You can configure the number of lines displayed for both the commands with option -n.

root@jaibhim:~# tail -n 3 greetings1.txt
13 Jai Bhim
14 Jai Bhim
15 Jai Bhim    

root@jaibhim:~# head -n 3 greetings1.txt
1 Jai Bhim
2 Jai Bhim
3 Jai Bhim

Both commands can be used for multiple files, as well. I give the name of both files with a space between them, and it prints the output. In the below example, I’ve used the option -n to restrict the output. If I skip the -n option, the commands print the default ten lines of each file. As displayed below, I’ve used more than one file as an argument, and both commands print a one-line header before the output of each file:

root@jaibhim:~# tail -n 3 greetings1.txt greetings2.txt
==> greetings1.txt <==
13 Jai Bhim
14 Jai Bhim
15 Jai Bhim
==> greetings2.txt <==
13 Good Morning
14 Good Morning
15 Good Morning
root@jaibhim:~# head -n 3 greetings1.txt greetings2.txt
==> greetings1.txt <==
1 Jai Bhim
2 Jai Bhim
3 Jai Bhim
==> greetings2.txt <==
1 Good Morning
2 Good Morning
3 Good Morning

The most important option for the tail command is -f. In some cases, when users execute a job or script, it creates log or error files. I need to track the log file to determine whether the respective script is working fine or not. At this time, I need to use the option -f with the tail command, which prints the output of the new lines that are being created by the script. Another example where you can use the option -f with the tail command is /var/log/messages file.

Here is a simple script, which runs in the background and adds single words at each line with a delay of one second:

for i in {16..100}; do echo “$i  JaiBhim” >> greetings1.txt; sleep 1; done &

Note: I’ve used & at the end of the command so that the script runs in the background.

Now, on the same terminal, use the tail command with option -f and the file name as an argument. You can see the new line is getting added in the same file:

tail -f greetings1.txt

Wrap up

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. Give them a try!

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]

Topics:   Linux  
Author’s photo

Amit Waghmare

I'm a techie guy with lots of love for Linux. I've started my career with a US-based project as Linux Administrator. Later, I got an opportunity to work with HPC clusters, where I learned several other products. More about me

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX