一天一个 Linux 命令(13):tail 命令

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了一天一个 Linux 命令(13):tail 命令脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.COM/article/131

一、简介

Linux里面我们经常看文件所需要到的其中一个命令:tail 命令,从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,只要 filename 更新就可以看到最新的文件内容.

二、格式说明

tail [OPTION]... [FILE]...
tail [选项] ... [文件]...

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

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.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

GNU coreutils online help: <http://www.gnu.org/Software/coreutils/>
For complete documentation, run: info coreutils 'tail invocation'

三、选项说明

-c<数目> 显示的字节数
-f 循环读取
-q 不显示处理信息
-n<行数> 显示文件的尾部 n 行内容
--pid=PID 与-f合用,表示在进程ID,PID死掉之后结束
-q, --quiet, --silent 从不输出给出文件名的首部
-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒
-v 显示详细的处理信息

四、命令功能

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

五、常见用法

1.显示 php-FPM.access.log文件末尾的内容,默认显示文件最后10行的内容

tail php-fpm.access.log

2.显示php-fpm.access.log文件末尾最后5行的数据

tail -n 5 php-fpm.access.log

3.动态循环显示文件末尾的内容

tail -f php-fpm.access.log

注意:可以按(Ctrl+C)组合键停止显示

4.显示文件 php-fpm.access.log的内容,从第 20 行至文件末尾

tail -n +20 php-fpm.access.log

5.显示文件 php-fpm.access.log 的最后20个字符

tail -c 20 php-fpm.access.log

 

本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/131

脚本宝典总结

以上是脚本宝典为你收集整理的一天一个 Linux 命令(13):tail 命令全部内容,希望文章能够帮你解决一天一个 Linux 命令(13):tail 命令所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。