订阅 RSS 源
Linux 

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. ]


关于作者

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. I love to teach, write blogs, troubleshoot complex issues, and write scripts to automate tasks. I also love to read books and watch movies/web series.

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来