每天一个linux命令14-tail

1. 命令解析

命令用途

把文件的末尾N行显示到标准输出,默认显示10行,还可通过 tail -f file 来持续输出文件的数据。

命令格式

tail [OPTION]... [FILE]...

命令参数

-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

2. 示例

2.1 显示文件末尾10行

[root@test headTest]# tail hisotry.log 
  213  less history.log 
  214  cd /root/test
  215  ls
  216  mkdir headTest
  217  ls
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.2 显示文件末尾5行 -n

[root@test headTest]# tail -5 hisotry.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.3 实时跟踪文件(默认1秒刷新一次) -f

[root@test headTest]# ping localhost > ping.log&
[1] 4971
[root@test headTest]# tail -f ping.log
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.011 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.051 ms

通过CTRL+C结束实时监控

2.4 定时实时跟踪文件 -fs

[root@test headTest]# ping localhost > ping.log&
[2] 5116
[root@test headTest]# tail -fs2 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=10 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=220 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=221 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=222 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=223 ttl=64 time=0.081 ms

每隔2秒刷新,显示文件最新内容

2.5 从第5行开始显示剩余所有内容 -n +N

[root@test headTest]# tail -n +5 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.081 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.050 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.029 ms
...省略...

2.6 显示多个文件

root@test headTest]# tail -n 5 hisotry.log ping.log 
==> hisotry.log <==
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

==> ping.log <==
64 bytes from localhost (127.0.0.1): icmp_seq=331 ttl=64 time=0.062 ms
64 bytes from localhost (127.0.0.1): icmp_seq=332 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=333 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=334 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=335 ttl=64 time=0.025 ms

2.7 不显示文件名 -q

[root@test headTest]# tail -qn 5 hisotry.log ping.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log
64 bytes from localhost (127.0.0.1): icmp_seq=373 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=374 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=375 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=376 ttl=64 time=0.032 ms
64 bytes from localhost (127.0.0.1): icmp_seq=377 ttl=64 time=0.030 ms
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,112评论 18 139
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,041评论 2 34
  • 我是一个喜欢在家吃饭的人,一般不太下馆子。可是什么吃多了都会腻,下馆子就作为调剂,偶尔沾些大油荤腥,放纵一下肠胃,...
    Betty0614203阅读 209评论 0 0
  • 夜。 无星光闪烁, 只见小飞虫在路灯下晃动。 无微风徐来, 只听老蝉不停歇的嘶喊。 夜, 枝头一抹绚烂, 悄悄吐露...
    冬至而已阅读 109评论 1 1
  • 《孩子,把你的手给我》D2学而思20170112 “语言暴力像一把消音枪, 谋杀了孩子的尊严和人生, 除了当时的痛...
    遥远晓然阅读 749评论 0 0