前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tail 命令详解

tail 命令详解

作者头像
全栈程序员站长
发布2022-09-05 10:42:55
1.1K0
发布2022-09-05 10:42:55
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

一、tail 命令介绍

tail 命令可以将文件指定位置到文件结束的内容写到标准输出。

如果你不知道tail命令怎样使用,可以在命令行执行命令tail --help就能看到tail命令介绍和详细的参数使用介绍,内容如下(我帮大家翻译了一下)。

代码语言:javascript
复制
[root@yanggongzi ~]# tail --help
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.
将每个文件的最后10行打印到标准输出。
如果有多个文件,在每个文件之前都有一个给出文件名的头文件。
没有文件,或者当文件为-时,读取标准输入。

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
                           输出最后的 K 个字节;
			               或者使用 -c +K 从每个文件的第K字节开始打印。
  -f, --follow[={ 
   name|descriptor}]
                           output appended data as the file grows;
                           an absent option argument means 'descriptor'
                           随着文件的增长,输出附加数据;(动态输出最新的信息);
                           没有选项参数意味着“描述符”
                           
  -F                       same as --follow=name --retry
                           与 --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
                           输出最后的K行,而不是最后的10行;
                           或者使用-n +K从第K个开始输出
                           
      --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
                           使用——follow=name,在N次(默认为5次)迭代后,重新打开一个大小没有改变的文件,看看它是否被解除链接或重命名(这是旋转日志文件的常见情况);
                           对于inotify,这个选项很少有用
                             
      --pid=PID            with -f, terminate after process ID, PID dies
                           与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令
                           
  -q, --quiet, --silent    never output headers giving file names
                           当有多个文件参数时,不输出各个文件名
                           
      --retry              keep trying to open a file if it is inaccessible
                           即是在tail命令启动时,文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件。使用此选项时需要与选项“——follow=name”连用
                           
  -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
                           与“-f”选项连用,指定监视文件变化时间隔的秒数(默认为1.0);
			               使用inotify和-pid=P,每N秒检查进程P至少一次
			               
  -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.
如果K前面的字符(字节数或行数)是'+',每个文件从第K项开始打印,否则,打印文件中最后的K项。K可能有一个乘数后缀:b 512,kB 1000,K 1024,MB 1000 1000,M 1024 1024,GB 1000 1000,G 1024 1024 1024,等等,对于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.
使用——follow (-f), tail默认跟随文件描述符,这意味着即使重命名了尾部文件,tail也将继续跟踪其尾部。
当您真正想要跟踪文件的实际名称而不是文件描述符(例如,日志旋转)时,这种默认行为是不可取的。
在这种情况下使用——follow=name。这将导致tail以一种适合重命名、删除和创建的方式跟踪已命名文件。

二、tail 命令使用示例

1、输出最后200个字符

在这里插入图片描述
在这里插入图片描述

2、从第900个字符开始输出,一直到最后

在这里插入图片描述
在这里插入图片描述

3、输出最后20行

在这里插入图片描述
在这里插入图片描述

4、从第36行开始输出,一直到最后

在这里插入图片描述
在这里插入图片描述

5、输出指定文件的最后十行,同时继续监视文件内容有无变化,新增内容会继续输出,直到按下 [Ctrl-C] 组合键退出

在这里插入图片描述
在这里插入图片描述

6、指定多个文件并输出文件名

在这里插入图片描述
在这里插入图片描述

7、指定多个文件不输出文件名

在这里插入图片描述
在这里插入图片描述

三、tailftail -ftail -F 的区别

  • tail -f

等同于–follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止

  • tail -F

等同于–follow=name –retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪

  • tailf

等同于tail -f -n 10(貌似tail -f或-F默认也是打印最后10行,然后追踪文件),与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件,所以tailf特别适合那些便携机上跟踪日志文件,因为它减少了磁盘访问,可以省电

当我们设置了滚动日志时,需要持续实时监控最新的日志输出,那么就要用tail -F,而不能用tailftail -f。因为当日志xxx.log达到了设定阈值重命名成了xxx01.log时,后两个命令追踪的还是xxx01.log文件而不是新创建的xxx.log文件,这时就不能继续监控最新日志了。

四、常用快捷键

【Ctrl】+【S】 暂停刷新。 【Ctrl】+【Q】继续刷新。 【Ctrl】+【C】退出 tail 命令。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135967.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年6月4,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、tail 命令介绍
  • 二、tail 命令使用示例
    • 1、输出最后200个字符
      • 2、从第900个字符开始输出,一直到最后
        • 3、输出最后20行
          • 4、从第36行开始输出,一直到最后
            • 5、输出指定文件的最后十行,同时继续监视文件内容有无变化,新增内容会继续输出,直到按下 [Ctrl-C] 组合键退出
              • 6、指定多个文件并输出文件名
                • 7、指定多个文件不输出文件名
                • 三、tailf、tail -f、tail -F 的区别
                • 四、常用快捷键
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档