Linux commands must learn (15) tail commands every day

Time:2020-6-27

The Linux tail command is used to output the last part of the specified file to the standard device according to the requirements, usually the terminal. Generally speaking, it is to display the last lines of a file to the terminal. Assuming that the file is updated, tail will actively refresh itself to ensure that you can see the latest file content.
Tail command syntax

Copy code

The code is as follows:

tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

Parameter interpretation:
-F this parameter is used to monitor file growth.
-C number reads the specified file from the number byte location
-N number reads the specified file from the number line location.
-M number reads the specified file from the number multi byte character position. For example, if your file contains Chinese characters, if you specify the – C parameter, it may cause truncation, but using – M will avoid this problem.
-B number reads the specified file from the 512 byte block location represented by number.
-K number reads the specified file from the 1KB block location represented by number.
File specifies the target file name for the operation
In the above commands, number is involved. If it is not specified, 10 lines will be displayed by default. Number can be preceded by a sign to indicate whether the offset is calculated from the top or the tail.
The tail runnable file is usually under / usr / bin /.
1. Command format;
Tail [required parameters] [select parameters] [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. Frequently view log files.
3. Command parameters:
-F loop read
-Q do not display processing information
-V display detailed processing information
-C < number > number of bytes displayed
-N < rows > display rows
–PID = PID and – F are used together, indicating the end after the process ID and PID are dead
-q. — quiet, – silent never outputs the first part of a given filename
-s. — sleep interval = s and – F are used together, which means sleep for s seconds at the interval of each repetition
4. Examples:
Example 1: display the content at the end of the file
Command:
tail -n 5 log2014.log
Output:
[[email protected] test]# tail -n 5 log2014.log
2014-09
2014-10
2014-11
2014-12
==============================[[email protected] test]#
explain:
Display the last 5 lines of the file
Example 2: loop through file contents
Command:
tail -f test.log
Output:
[[email protected] ~]# ping 192.168.120.204 > test.log &
[1] 11891[[email protected] ~]# 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
[[email protected] ~]#
explain:
ping 192.168.120.204 > test.log &/ / ping the remote host in the background. And output files to test.log ; this method is also used for monitoring more than one file. Use Ctrl + C to terminate.
Example 3: display file from line 5
Command:
tail -n +5 log2014.log
Output:
[[email protected] 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
==============================
[[email protected] test]# tail -n +5 log2014.log
2014-05
2014-06
2014-07
2014-08
2014-09
2014-10
2014-11
2014-12
==============================