Linux教程

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

本文主要是介绍一天一个 Linux 命令(13):tail 命令,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、简介

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

 

这篇关于一天一个 Linux 命令(13):tail 命令的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!