Linux(Centos 7.6)命令详解:tail

1.命令作用

将每个文件的最后10行打印到标准输出(Print the last 10 lines of each FILE to standard output)

2.命令语法

Usage: tail [OPTION]... [FILE]...

3.参数详解

OPTION:

  • -c, --bytes=K,输出最后K字节
    • -c +nK,从文件的第n字节开始到尾部内容输出到屏幕
  • -f, --follow[={name|descriptor}],随着文件的增长输出附加数据,缺少选项参数则为'descriptor';(当为name时,查看文件重命名后,生成新文件,会持续输出;当为descriptor时则不会持续输出,主要用于日志文件轮转/重命名场景)
  • -F,与--follow=name --retry相同
  • -n, --lines=K,输出最后K行而不是最后10行
    • -n +nK,从文件的第n行开始到尾部内容输出到屏幕
  • --max-unchanged-stats=N,与--follow=name参数一起使用,在N次(默认为5次)迭代后重新打开没有更改大小的文件,以查看它是否已被解除链接或重命名《无明显效果》
  • --pid=PID,与-f参数一起使用,在进程ID,PID死亡后结束
  • -q, --quiet, --silent,不要打印带有文件名的头文件(默认参数)
  • --retry,与--follow=name参数一起使用,如果无法访问,请继续尝试打开文件
  • -s, --sleep-interval=N,与-f参数一起使用,在迭代之间休眠大约N秒(默认1.0秒)《无明显效果》
  • -v, --verbose,总是打印给出文件名的头文件(与-q参数对立)

4.常用用例

4.1.显示末尾5行(默认是10行)

[root@localhost Desktop]# tail -n 5 /etc/passwd   ## 指定显示末尾5行
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
qwer:x:1000:1000:qwer:/home/qwer:/bin/bash
[root@localhost Desktop]# 
[root@localhost Desktop]# tail /etc/passwd        ## 默认显示末尾10行
setroubleshoot:x:991:985::/var/lib/setroubleshoot:/sbin/nologin
saned:x:990:984:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
qwer:x:1000:1000:qwer:/home/qwer:/bin/bash
[root@localhost Desktop]# 

4.2.持续显示文件末尾内容

当文件内容追加时,用于查看内容,一般用于查看日志文件

[root@localhost Desktop]# tail -f /etc/passwd
setroubleshoot:x:991:985::/var/lib/setroubleshoot:/sbin/nologin
saned:x:990:984:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
qwer:x:1000:1000:qwer:/home/qwer:/bin/bash

4.3.--follow=name参数

文件重命名后,重新生成文件,会持续查看文件末尾内容;--follow=descriptor参数则相反,文件重命名后,重新生成文件,不会持续查看文件末尾内容

## 1.窗口一执行如下命令后,窗口二重命名文件ccc为ddd
[root@localhost Desktop]# tail --follow=name ccc   
asdfasdfasdf

## 2.重命名后窗口一效果
[root@localhost Desktop]# tail --follow=name ccc
asdfasdfasdf
tail: ccc: No such file or directory

## 3.窗口二创建新ccc文件后窗口一效果
[root@localhost Desktop]# tail --follow=name ccc
asdfasdfasdf
tail: ccc: No such file or directory
tail: ‘ccc’ has appeared;  following end of new file

## 4.窗口二往文件ccc写入内容,窗口一效果
[root@localhost Desktop]# tail --follow=name ccc
asdfasdfasdf
tail: ccc: No such file or directory
tail: ‘ccc’ has appeared;  following end of new file
aaaaaaaaaaaaaaa

####################################################################
## 窗口二的执行命令
[root@localhost Desktop]# mv ccc ddd 
[root@localhost Desktop]# touch ccc
[root@localhost Desktop]# echo 'aaaaaaaaaaaaaaa' >> ccc
[root@localhost Desktop]# 

4.4.--pid=PID参数

## 窗口一执行ping 192.168.118.1
[root@localhost Desktop]# ping 192.168.118.1
PING 192.168.118.1 (192.168.118.1) 56(84) bytes of data.
64 bytes from 192.168.118.1: icmp_seq=1 ttl=128 time=0.423 ms
64 bytes from 192.168.118.1: icmp_seq=2 ttl=128 time=1.52 ms
... ...

## 窗口二执行ping 192.168.118.201 >> ccc
[root@localhost Desktop]# ping 192.168.118.201 >> ccc


## 窗口三查看进程pid
[root@localhost Desktop]# ps -ef | grep ping
root      10562  10149  0 Feb13 ?        00:00:00 /usr/libexec/gsd-housekeeping
root      12363  10923  0 01:30 pts/1    00:00:00 ping 192.168.118.1
root      12422  11559  0 01:31 pts/2    00:00:00 ping 192.168.118.201
root      12426  11879  0 01:31 pts/3    00:00:00 grep --color=auto ping
[root@localhost Desktop]# 

## 窗口四tail使用参数-f --pid查看ccc文件
[root@localhost Desktop]# tail -f --pid=12363 ccc
64 bytes from 192.168.118.201: icmp_seq=3 ttl=64 time=0.055 ms
64 bytes from 192.168.118.201: icmp_seq=4 ttl=64 time=0.052 ms
... ...

## 窗口三kill -9 12363 进程,kill后窗口四输出结束
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值