tail使用

1 查看文件的最后10行内容

tail

head命令可以查看一个文件前10行的内容,而按照典型的异想天开式的Unix风格,还可以使用tail命令来查看一个文件最后10行的内容。

$ tail Paradise_Lost.txt
To the subjected plain - then disappeared
They, looking back, all the eastern side beheld
Of Paradise, so late their happy seat,
Waved over by that flaming brand; the gate
With dreadful faces thronged and fiery arms.
Some natural tears they dropped, but wiped them soon;
The world was all before them, where to choose
Their place of rest, and Providence their guide.
They, hand in hand, with wandering steps and slow,
Through Eden took their solitary way.

 

为什么要使用tail命令呢?通常,查看日志(log)文件的最后几行内容,才能发现应用程序或者系统的最新运行状况。

2 查看多个文件的最后10行内容

tail file1 file2

使用head命令可以一次查看多个文件的前10行内容,同样,使用tail命令也可以做类似的事

$ tail Paradise_Lost.txt Miller's_Tale.txt
==> Paradise_Lost.txt <==
To the subjected plain - then disappeared
They, looking back, all the eastern side beheld
Of Paradise, so late their happy seat,
Waved over by that flaming brand; the gate
With dreadful faces thronged and fiery arms.
Some natural tears they dropped, but wiped them soon;
The world was all before them, where to choose
Their place of rest, and Providence their guide.
They, hand in hand, with wandering steps and slow,
Through Eden took their solitary way.
==> Miller's_Tale.txt <==
With othes grete he was so sworn adoun
That he was holde wood in al the toun;
For every clerk anonright heeld with oother.
They seyde, “The man is wood, my leeve brother”;
And every wight gan laughen at this stryf.
Thus swyved was this carpenteris wyf,
For al his kepyng and his jalousye;
And Absolon hath kist hir nether ye;
And Nicholas is scalded in the towte.
This tale is doon, and God save al the rowte!

 

和head命令一样,tail命令也使用标题(分隔符)来分隔不同的文件。

3 查看一个或多个文件的后面几行内容

tail -n

除了接受默认的10行,也可以使用-n(或--lines=)选项来指定想要查看的文件行数。想要查看多个文件的后面几行?只要把多个文件名加到命令中就可以了。

$ tail -n 4 Paradise_Lost.txt Miller's_Tale.txt
==> Paradise_Lost.txt <==
The world was all before them, where to choose
Their place of rest, and Providence their guide.
They, hand in hand, with wandering steps and slow,
Through Eden took their solitary way.
==> Miller's_Tale.txt <==
For al his kepyng and his jalousye;
And Absolon hath kist hir nether ye;
And Nicholas is scalded in the towte.
This tale is doon, and God save al the rowte!

 

4 查看一个或多个文件中不断更新的最后几行

tail –f

tail -f --pid=PID# terminates after PID dies.

日志文件的最大特点就是,它会随着系统中各种事情的发生而不断更新。tail命令可以显示文件的一个快照(snapshot),然后直接返回到命令行。想要再次查看日志文件?不断地运行tail命令,一次,又一次。比较麻烦!

使用-f(或--follow)选项,tail命令就不会自动关闭了。每当文件发生变化时,它就会显示文件的最后10行(或在选项中增加-n,指定不同的数字)的内容。用这种方法就可以观察到日志文件随时发生的所有变化。如果你正想弄明白系统或者程序刚刚发生了什么情况,这个命令就非常有用。

例如,Web服务器的日志文件可能如下所示:

说明 为了节省篇幅,删除了访问的IP地址、日期和时间。

$ tail -f /var/log/httpd/d20srd_org_log_20051201
“GET /srd/skills/bluff.htm HTTP/1.1”...
“GET /srd/skills/senseMotive.htm HTTP/1.1”...
“GET /srd/skills/concentration.htm HTTP/1.1”...
“GET /srd/classes/monk.htm HTTP/1.1”...
“GET /srd/skills/escapeArtist.htm HTTP/1.1”...

 

tail命令会一直打开它,以确保任何新的变化都能及时显示出来。文件显示的内容不断向上滚动,直到按Ctrl+c键才会停下来,并返回到命令行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hamilton_Huan

原创不易,结合业务原创更不易

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值