前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一天一个 Linux 命令(13):tail 命令

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

作者头像
joshua317
发布2021-09-26 11:07:39
2380
发布2021-09-26 11:07:39
举报
文章被收录于专栏:技术博文

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

一、简介

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

二、格式说明

代码语言:javascript
复制
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'

三、选项说明

代码语言:javascript
复制
-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行的内容
代码语言:javascript
复制
tail php-fpm.access.log
2.显示php-fpm.access.log文件末尾最后5行的数据
代码语言:javascript
复制
tail -n 5 php-fpm.access.log
3.动态循环显示文件末尾的内容
代码语言:javascript
复制
tail -f php-fpm.access.log

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

4.显示文件 php-fpm.access.log的内容,从第 20 行至文件末尾
代码语言:javascript
复制
tail -n +20 php-fpm.access.log
5.显示文件 php-fpm.access.log 的最后20个字符
代码语言:javascript
复制
tail -c 20 php-fpm.access.log

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、格式说明
  • 三、选项说明
  • 四、命令功能
  • 五、常见用法
    • 1.显示 php-fpm.access.log文件末尾的内容,默认显示文件最后10行的内容
      • 2.显示php-fpm.access.log文件末尾最后5行的数据
        • 3.动态循环显示文件末尾的内容
          • 4.显示文件 php-fpm.access.log的内容,从第 20 行至文件末尾
            • 5.显示文件 php-fpm.access.log 的最后20个字符
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档