tail 命令的功能是用于查看文件尾部内容,默认会在终端界面上显示出指定文件的末尾十行,如果指定了多个文件,则会在显示的每个文件内容前面加上文件名来加以区分。
目录
一、语法格式
tail [参数] 文件名
二、常用参数
-c | 指定输出文件尾部的字符数 |
-n | 指定输出文件尾部的行数 |
-f | 持续输出文件尾部最新内容 |
三、命令实战
3.1 默认显示指定文件最后10行
[root@linux1 ~]# tail /var/log/messages
Apr 23 13:11:16 linux1 kernel: e1000: ens32 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Apr 23 13:11:16 linux1 NetworkManager[783]: <info> [1682226676.2885] device (ens32): carrier: link connected
Apr 23 13:14:29 linux1 chronyd[803]: Selected source 119.28.183.184
Apr 23 13:16:38 linux1 chronyd[803]: Selected source 202.118.1.81
Apr 23 14:01:01 linux1 systemd: Started Session 5 of user root.
Apr 23 15:01:01 linux1 systemd: Started Session 6 of user root.
Apr 23 15:21:53 linux1 systemd-logind: New session 7 of user root.
Apr 23 15:21:53 linux1 systemd: Started Session 7 of user root.
Apr 23 15:22:34 linux1 systemd-logind: Removed session 1.
Apr 23 16:01:01 linux1 systemd: Started Session 8 of user root.
3.2 显示指定文件最后200个字节
[root@linux1 ~]# tail -c 200 /var/log/messages
of user root.
Apr 23 15:21:53 linux1 systemd: Started Session 7 of user root.
Apr 23 15:22:34 linux1 systemd-logind: Removed session 1.
Apr 23 16:01:01 linux1 systemd: Started Session 8 of user root.
3.3 显示指定文件第200个字节后所有内容
[root@linux1 ~]# tail -c +200 /var/log/messages
11:10 linux1 systemd: Time has been changed
Apr 23 13:11:14 linux1 chronyd[803]: Forward time jump detected!
Apr 23 13:11:14 linux1 chronyd[803]: Can't synchronise: no selectable sources
Apr 23 13:11:16 linux1 kernel: e1000: ens32 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Apr 23 13:11:16 linux1 NetworkManager[783]: <info> [1682226676.2885] device (ens32): carrier: link connected
Apr 23 13:14:29 linux1 chronyd[803]: Selected source 119.28.183.184
Apr 23 13:16:38 linux1 chronyd[803]: Selected source 202.118.1.81
Apr 23 14:01:01 linux1 systemd: Started Session 5 of user root.
Apr 23 15:01:01 linux1 systemd: Started Session 6 of user root.
Apr 23 15:21:53 linux1 systemd-logind: New session 7 of user root.
Apr 23 15:21:53 linux1 systemd: Started Session 7 of user root.
Apr 23 15:22:34 linux1 systemd-logind: Removed session 1.
Apr 23 16:01:01 linux1 systemd: Started Session 8 of user root.
3.4 显示指定文件最后5行
[root@linux1 ~]# tail -n 5 /var/log/messages
Apr 23 15:01:01 linux1 systemd: Started Session 6 of user root.
Apr 23 15:21:53 linux1 systemd-logind: New session 7 of user root.
Apr 23 15:21:53 linux1 systemd: Started Session 7 of user root.
Apr 23 15:22:34 linux1 systemd-logind: Removed session 1.
Apr 23 16:01:01 linux1 systemd: Started Session 8 of user root.
3.5 显示指定文件第15行后的所有内容
[root@linux1 ~]# tail -n +15 /var/log/messages
Apr 23 15:22:34 linux1 systemd-logind: Removed session 1.
Apr 23 16:01:01 linux1 systemd: Started Session 8 of user root.
3.6 显示指定多个文件并输出文件名
[root@linux1 ~]# tail -v 1.txt 2.txt
==> 1.txt <==
222
1eufhdjf
hello world
==> 2.txt <==
Hello world
Hello world
3.7 显示指定多个文件不输出文件名
[root@linux1 ~]# tail -q 1.txt 2.txt
222
1eufhdjf
hello world
Hello world
Hello world
3.8 高阶玩法,动态查看指定文件
默认输出指定文件最后十行,同时监视文件内容有无变化,如有新增继续输出到屏幕,直到 Ctrl+c 退出。