tail 命令
说明:tail命令属于文件类型的命令,
万能命令:man tail 或者 tail --help
用、说明、参数列表:
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows;
an absent option argument means 'descriptor'
-F same as --follow=name --retry
-n, --lines=K output the last K lines, instead of the last 10;
or use -n +K to output starting with the Kth
--max-unchanged-stats=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to see if it has been unlinked or renamed
(this is the usual case of rotated log files);
with inotify, this option is rarely useful
--pid=PID with -f, terminate after process ID, PID dies
-q, --quiet, --silent never output headers giving file names
--retry keep trying to open a file if it is inaccessible
-s, --sleep-interval=N with -f, sleep for approximately N seconds
(default 1.0) between iterations;
with inotify and --pid=P, check process P at
least once every N seconds
-v, --verbose always output headers giving file names
--help display this help and exit
--version output version information and exit
案例:以如下文件sort.txt为案例
tail sort.txt
[root@pdemo tail-test]# cat sort.txt
1
2
3
4
5
6
7
8
9
10
11
12
使用tail sort.txt 会显示最后10行
[root@pdemo tail-test]# tail sort.txt
3
4
5
6
7
8
9
10
11
12
-c 后面带容量 显示最后多少k的内容
[root@pdemo tail-test]# tail -c 10 sort.txt
10
11
12
检测是否是10k
[root@pdemo tail-test]# tail -c 10 sort.txt >tail.txt
[root@pdemo tail-test]# ll tail.txt
-rw-r--r-- 1 root root 10 Apr 8 21:37 tail.txt
-f 此参数查看日志实时内容 很方便
首先 写一个一直打印的命令到tailf.txt
while true; do echo `date` >>tailf.txt && sleep 1;done;
打开一个新的窗口 接收信息 发现可以实时更新内容
[root@pdemo tail-test]# tail -f tailf.txt
Wed Apr 8 21:44:43 CST 2020
Wed Apr 8 21:44:44 CST 2020
Wed Apr 8 21:44:45 CST 2020
Wed Apr 8 21:44:46 CST 2020
Wed Apr 8 21:44:47 CST 2020
Wed Apr 8 21:44:48 CST 2020
Wed Apr 8 21:44:49 CST 2020
Wed Apr 8 21:44:50 CST 2020
Wed Apr 8 21:44:51 CST 2020
Wed Apr 8 21:44:52 CST 2020
效果和tailf是一样的
[root@pdemo tail-test]# tailf tailf.txt
-n tail 默认是输出最后10行,但是-n参数可以对此进行调整
输出最后两行
[root@pdemo tail-test]# tail -n 2 tail.txt
11
12
输出最后三行
[root@pdemo tail-test]# tail -n 3 tail.txt
10
11
12
tail -n +数字 file 输出最后数字的行
-q 和-s 经过测试 暂时没有发现啥特点 暂时不在这里写出