5.tail命令用法

189 阅读4分钟

五.tail

1.命令介绍

tail命令和head命令一样,使用比较频繁,尤其是在调试日志的时候用的比较多。
接下来我们一起探讨下tail命令的使用

tail命令的功能是用于查看文件尾部内容,例如默认会在终端界面上显示出指定文件的末尾十行,如果指定了多个文件,则会在显示的每个文件内容前面加上文件名来加以区分。

高阶玩法的-f参数作用是持续显示文件的尾部最新内容,类似于机场候机厅的大屏幕,总会把最新的消息展示给用户,对阅读日志文件尤为适合,而不需要手动刷新。

2.常用参数

image.png

可以使用tail --help命令查看所有参数

[root@jichu ~]# tail --help
用法:tail [选项]... [文件]...
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		显示此帮助信息并退出
      --version		显示版本信息并退出

If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

如果您希望即时追查一个文件的有效名称而非描述内容(例如循环日志),默认
的程序动作并不如您所愿。在这种场合可以使用--follow=name 选项,它会使
tail 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。
3.案例示范
3.1 输出后10行
[root@jichu ~]# tail /etc/passwd |wc -l
10
3.2 输出最后100个字符
[root@jichu ~]# tail -c 100 /etc/passwd
n
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@jichu ~]# 
3.3 从第100个字符开始输出,一直到最后
[root@jichu ~]# tail -c +100 /etc/passwd
login
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@jichu ~]# 
3.4 从第12行开始一直到最后
[root@jichu ~]# tail -n +12 /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@jichu ~]# 
3.5 输出文件的最后十行并监控文件内容的变化

这里就可以使用tail -f动态的监控日志的变化。

image.png

4.tailf, tail -f , tail -Fd 区别
  • tail -f
    等同于–follow=descriptor,
    常用于日志内容的跟踪,根据文件描述符进行追踪,当文件改名或被删除,追踪停止
  • tail -F
    等同于–follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪
  • tailf
    等同于tail -f -n 10
    与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件。

最后:

tailf特别适合那些便携机上跟踪日志文件,因为它减少了磁盘访问,节省了资源。

当我们设置了滚动日志时,需要持续实时监控最新的日志输出,那么就要用tail -F,而不能用tailf 和 tail -f。

滚动日志达到一定的阈值就会重新命名,这三个命令中只有tail -F可以在重新命名后继续追踪。

♥.gif