The tail command starts writing the file to the standard output from the specified point. Use the -f option of the tail command to easily view the log file that is being changed. tail -f filename will display the last content in filename on the screen, and not only refresh , so you can see the latest file contents.
1. command format;
tail[required parameter][optional parameter][file]
2. Command function:
It is used to display the content at the end of the specified file. When no file is specified, it is processed as input information. Commonly viewed log files.
3. Command parameters:
-f loop read
-q do not display processing information
-v display detailed processing information
-c <number> number of bytes to display
-n<number of lines> Display the number of lines
--pid=PID is used with -f, which means that it will end after the process ID and PID die.
-q, --quiet, --silent never output headers given filenames
-s, --sleep-interval=S Combined with -f, it means to sleep for S seconds at the interval of each repetition
4. Example of use:
Example 1: Display the content at the end of the file
Order:
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]#
illustrate:
Display the last 5 lines of the file
Example 2: Loop through the file content
Order:
tail -f test.log
output:
[root@localhost ~]# ping 192.168.120.204 > test.log & //This & means that you can execute other commands in the terminal next, and print them in the background; otherwise, nothing will be displayed and blocked there
[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 ~]#
illustrate:
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 file monitoring. Use Ctrl+c to terminate.
Example 3: Display the file starting from line 5
Order:
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
==============================