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.
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래