zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux基本功系列之tail命令实战

Linux命令 系列 实战 tail 基本功
2023-09-14 09:09:25 时间

系列文章目录

命令1: linux基本功系列-ls命令实战
命令2: linux基本功系列之echo命令实战
命令3:linux基本功之历史记录history命令实战
命令4: linux基本功之date命令实战
命令5 linux基本功之touch命令实战
命令6 linux基本功系列之mkdir命令实战
命令7 linux基本功系列之最危险的命令rm
命令8 linux基本功系列之cp命令实战
命令9 linux基本功系列之cat命令实战
命令10 linux基本功系列之mv命令实战
命令11 linux基本功系列之head命令实战
命令12 linux基本功系列之tail命令实战



前言

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


一. tail命令的介绍

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

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

二、常用参数

在这里插入图片描述

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

[root@mufenggrow ~]# 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 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。


三. tail 使用案例示范

3.1 输出后10行

[root@mufenggrow ~]# tail /etc/passwd |wc -l
10

3.2 输出最后100个字符

[root@mufenggrow ~]# tail -c 100 /etc/passwd
gin
laoxin:x:1000:1000:laoxin:/home/laoxin:/bin/bash
itlaoxin:x:1001:1001::/home/itlaoxin:/bin/bash
[root@mufenggrow ~]# 

3.3 从第100个字符开始输出,一直到最后

[root@mufenggrow ~]# tail -c +100 /etc/passwd
login
adm:x:3:4:adm:/var/adm:/sbin/nologin

3.4 从第43行开始一直到最后

[root@mufenggrow ~]# tail -n +43 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin


3.5 输出文件的最后十行并监控文件内容的变化

这里就可以使用tail -f动态的监控日志的变化。
在这里插入图片描述

四. 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可以在重新命名后继续追踪。


总结

以上就是对tail命令的总结,linux基础命令150个,已经更新到第12个,继续加油。