A Linux command a day (15): tail command

post thumb
Devops
by Admin/ on 27 Sep 2021

A Linux command a day (15): tail command


The tail command writes a file to standard output from the specified point. Use the -f option of the tail command to easily access the changing log file. tail -f filename will display the most trailing content of filename on the screen and refresh it so you can see the latest file content.

1. Command Format:

tail [required parameters][selected parameters][file]

2. Command function.

It is used to display the content of the end of the specified file, and is processed as input information when no file is specified. Commonly used to view log files.

3. Command parameters.

-f Read cyclically

-q does not show the processing information

-v Show detailed processing information

-c<number> Number of bytes displayed

-n<number of lines> show the number of lines

--pid=PID Used in conjunction with -f to end after the process ID,PID dies.  
-q, --quiet, --silent Never output the first part of the given filename 

-s, --sleep-interval=S used in conjunction with -f to sleep for S seconds at each iteration 

4. Examples of use.

Example 1: Displaying the end-of-file contents

Command:

tail -n 5 log2014.log

output:

[root@localhost test]# tail -n 5 log2014.log   
2014-09  
2014-10  
2014-11  
2014-12  
==============================  
[root@localhost test]#  

Description:

Display the last 5 lines of the file

Example 2: Loop through the contents of a file

Commands:

tail -f test.log

output:

[root@localhost ~]# ping 192.168.120.204 > test.log &  
[1] 11891[root@localhost ~]# tail -f test.log   
PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.  
64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms  
64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms  
64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms  
64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms  
64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms  
64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms  
64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms  
64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms  
64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms  
64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms  
64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms  
[root@localhost ~]#  

Description:

ping 192.168.120.204 > test.log & // ping the remote host in the background. and output the file to test.log; this practice is also used for more than one archive monitoring. Use Ctrl + c to terminate.

Example 3: Show files from line 5

Command:

tail -n +5 log2014.log

output:

[root@localhost test]# cat log2014.log   
2014-01  
2014-02  
2014-03  
2014-04  
2014-05  
2014-06  
2014-07  
2014-08  
2014-09  
2014-10  
2014-11  
2014-12  
==============================  
[root@localhost test]# tail -n +5 log2014.log  
2014-05  
2014-06  
2014-07  
2014-08  
2014-09  
2014-10  
2014-11  
2014-12  
==============================  

Reference:

www.cnblogs.com/peida/archive/2012/11/07/2758084.html

comments powered by Disqus