Linux tail 命令使用总结

命令描述
tail命令可以将指定点到文件结束的内容写到标准输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件。tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新,使你看到最新的文件内容。

命令语法
tail [ -f ] [ -c Number | -n Number | -m Number | -b Number | -k Number ] [ File ]

常用的参数介绍

-f
输出指定文件的最后十行,同时继续监视文件内容有无变化,新增内容会继续输出,直到按下 [Ctrl-C] 组合键停止该命令。

默认的tail -f 输出文件末尾10行的信息:


[root@localhost ~]# tail -f /var/log/nilex/clusterhead.log
0306165235.037485-1154984256 Cleanup 0 closed sessions
0306165235.066998-1136073024 No content(0) to be duplicated
0306165236.836391-1163389248 [WARNING]: Failed to send cmd 399 to 192.168.14.14:9560
0306165236.836417-1163389248 [ERROR]: [chnl_ctrl_pull_timeout] Failed to pull channel: -13
0306165250.034631-1157085504 Start cleanup closed sessions
0306165250.034662-1157085504 Cleanup 0 closed sessions
0306165305.034703-1154984256 Start cleanup closed sessions
0306165305.034733-1154984256 Cleanup 0 closed sessions
0306165320.034771-1157085504 Start cleanup closed sessions
0306165320.034804-1157085504 Cleanup 0 closed sessions

等效的命令:


[root@localhost ~]# tail –follow install.log.syslog
<86>Feb 14 13:51:31 useradd[3050]: new group: name=xfs, GID=43
<86>Feb 14 13:51:31 useradd[3050]: new user: name=xfs, UID=43, GID=43, home=/etc/X11/fs, shell=/sbin/nologin
<86>Feb 14 13:51:39 useradd[3773]: new group: name=rpcuser, GID=29
<86>Feb 14 13:51:39 useradd[3773]: new user: name=rpcuser, UID=29, GID=29, home=/var/lib/nfs, shell=/sbin/nologin
<86>Feb 14 13:51:39 groupadd[3781]: new group: name=nfsnobody, GID=65534
<86>Feb 14 13:51:39 useradd[3788]: new user: name=nfsnobody, UID=65534, GID=65534, home=/var/lib/nfs, shell=/sbin/nologin
<86>Feb 14 13:51:47 useradd[3831]: new group: name=haldaemon, GID=68
<86>Feb 14 13:51:47 useradd[3831]: new user: name=haldaemon, UID=68, GID=68, home=/, shell=/sbin/nologin
<86>Feb 14 13:51:47 groupadd[3840]: new group: name=avahi-autoipd, GID=102
<86>Feb 14 13:51:47 useradd[3845]: new user: name=avahi-autoipd, UID=100, GID=102, home=/var/lib/avahi-autoipd, shell=/sbin/nologin

-n Number
从文件的末尾倒数Number行开始输出文件内容。


[root@localhost ~]# tail -n 10 install.log.syslog
<86>Feb 14 13:51:31 useradd[3050]: new group: name=xfs, GID=43
<86>Feb 14 13:51:31 useradd[3050]: new user: name=xfs, UID=43, GID=43, home=/etc/X11/fs, shell=/sbin/nologin
<86>Feb 14 13:51:39 useradd[3773]: new group: name=rpcuser, GID=29
<86>Feb 14 13:51:39 useradd[3773]: new user: name=rpcuser, UID=29, GID=29, home=/var/lib/nfs, shell=/sbin/nologin
<86>Feb 14 13:51:39 groupadd[3781]: new group: name=nfsnobody, GID=65534
<86>Feb 14 13:51:39 useradd[3788]: new user: name=nfsnobody, UID=65534, GID=65534, home=/var/lib/nfs, shell=/sbin/nologin
<86>Feb 14 13:51:47 useradd[3831]: new group: name=haldaemon, GID=68
<86>Feb 14 13:51:47 useradd[3831]: new user: name=haldaemon, UID=68, GID=68, home=/, shell=/sbin/nologin
<86>Feb 14 13:51:47 groupadd[3840]: new group: name=avahi-autoipd, GID=102
<86>Feb 14 13:51:47 useradd[3845]: new user: name=avahi-autoipd, UID=100, GID=102, home=/var/lib/avahi-autoipd, shell=/sbin/nologin

linux下的帮助文档:

[root@localhost ~]# man tail

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.

–retry
keep trying to open a file even if it is inaccessible when tail starts or if it becomes inaccessible later; use-ful when following by name, i.e., with –follow=name

-c, –bytes=N
output the last N bytes

-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=N
output the last N lines, instead of the last 10

–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)

–pid=PID
with -f, terminate after process ID, PID dies

-q, –quiet, –silent
never output headers giving file names

-s, –sleep-interval=S
with -f, sleep for approximately S seconds (default 1.0) between iterations.

-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 N (the number of bytes or lines) is a ¡®+¡¯, print beginning with the Nth item from the start of each file, otherwise, print the last N items in the file. N may have a multiplier suffix: b 512, k 1024, m 1024*1024.

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 by reopening it periodically to see if it has been removed and recreated by some other program.

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

此条目发表在lnmpa分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据