Linux tail 命令详解

17 篇文章 0 订阅
16 篇文章 0 订阅

转载自:http://tk-zhang.iteye.com/blog/511153

Linux tail 命令详解

用途
从指定点开始将文件写到标准输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容。
语法
标准语法
tail [  -f ] [  -c Number |  -n Number |  -m Number |  -b Number |  -k Number ] [ File ]
要以逆序显示行
tail [  -r ] [  -n Number ] [ File ]
描述
tail命令从指定点开始将 File 参数指定的文件写到标准输出。如果没有指定文件,则会使用标准输入。 Number 变量指定将多少单元写入标准输出。Number 变量的值可以是正的或负的整数。如果值的前面有 +(加号),从文件开头指定的单元数开始将文件写到标准输出。如果值的前面有-(减号),则从文件末尾指定的单元数开始将文件写到标准输出。如果值前面没有 +(加号)或 -(减号),那么从文件末尾指定的单元号开始读取文件。
Number 变量用于确定计数的起点的单元类型由 -b、-c、-k、-m 以及 -n 标志确定。如果没有指定其中的任何一个标志,那么 tail 命令就会读取指定文件的最后十行,并将其写到标准输出。这与在命令行输入 -n 10 是相同的。
-m 标志在单字节和双字节字符环境中提供了一致的结果。当输入是包含多字节字符的文本文件时应谨慎使用 -c 标志,因为产生的输出可能不从字符边界开始。

标志
-b Number
从 Number 变量表示的 512 字节块位置开始读取指定文件。

-c Number
从 Number 变量表示的字节位置开始读取指定文件。

-f
如果输入文件是常规文件或如果 File 参数指定 FIFO(先进先出),那么 tail命令不会在复制了输入文件的最后的指定单元后终止,而是继续从输入文件读取和复制额外的单元(当这些单元可用时)。如果没有指定 File参数,并且标准输入是管道,则会忽略 -f 标志。tail -f 命令可用于监视另一个进程正在写入的文件的增长。
-k Number
从 Number 变量表示的 1KB 块位置开始读取指定文件。
-m Number
从 Number 变量表示的多字节字符位置开始读取指定文件。使用该标志提供在单字节和双字节字符代码集环境中的一致结果。
-n Number
从 Number 变量表示的行位置开始读取指定文件。
-r
从文件末尾以逆序方式显示输出。-r 标志的缺省值是以逆序方式打印整个文件。如果文件大于 20,480 字节,那么 -r 标志只显示最后的 20,480 字节。
-r 标志只有与 -n 标志一起时才有效。否则,就会将其忽略。
退出状态
该命令返回下列的退出值:
0
成功完成。
>0
出现错误。
示例
要显示 notes 文件的最后十行,输入:
tail notes要指定从 notes 文件末尾开始读取的行数,输入:
tail  -n 20 notes
要从第 200 字节开始,每次显示一页 notes 文件,输入:
tail  -c +200 notes | pg
要跟踪文件的增长,输入:
tail  -f accounts
这显示 accounts 文件的最后十行。tail 命令继续显示添加到 accounts 文件中的行。显示会一直继续,直到您按下 Ctrl-C 按键顺序来停止。
文件
/usr/bin/tail
包含 tail 命令。

lester@lester-ThinkPad-T410:~/shell/tail$ cat tail.txt
TAIL(1)                                             User Commands                                            TAIL(1)
NAME
       tail - output the last part of files
SYNOPSIS
       tail [OPTION]... [FILE]...
DESCRIPTION
       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; alternatively[作为选择], use -c +K to output bytes starting with the Kth of each file

       -f, --follow[={name|descriptor}]
              output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent[等价的,相等的]

       -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 lines 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 even when it is or becomes inaccessible[达不到的,难达到的]; useful  when  following  by  name,
              i.e., with --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.

       -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  cre‐
       ation.

AUTHOR
       Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.

REPORTING BUGS
       Report tail bugs to bug-coreutils@gnu.org
       GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
       General help using GNU software: <http://www.gnu.org/gethelp/>
       Report tail translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright   ©   2011   Free   Software  Foundation,  Inc.   License  GPLv3+:  GNU  GPL  version  3  or  later
       <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent  per‐
       mitted by law.

SEE ALSO
       The  full  documentation for tail is maintained as a Texinfo manual.  If the info and tail programs are prop‐
       erly installed at your site, the command

              info coreutils 'tail invocation'

       should give you access to the complete manual.



GNU coreutils 8.12.197-032bb                       September 2011                                            TAIL(1)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值