linux基本功系列之tail命令实战
系列文章目录
命令1: linux基本功系列-ls命令实战
命令2: linux基本功系列之echo命令实战
命令3:linux基本功之历史记录history命令实战
命令4: linux基本功之date命令实战
命令5 linux基本功之touch命令实战
命令6 linux基本功系列之mkdir命令实战
命令7 linux基本功系列之最危险的命令rm
命令8 linux基本功系列之cp命令实战
命令9 linux基本功系列之cat命令实战
命令10 linux基本功系列之mv命令实战
命令11 linux基本功系列之head命令实战
命令12 linux基本功系列之tail命令实战
文章目录
前言
tail命令和head命令一样,使用比较频繁,尤其是在调试日志的时候用的比较多。
接下来我们一起探讨下tail命令的使用
一. tail命令的介绍
tail命令的功能是用于查看文件尾部内容,例如默认会在终端界面上显示出指定文件的末尾十行,如果指定了多个文件,则会在显示的每个文件内容前面加上文件名来加以区分。
高阶玩法的-f参数作用是持续显示文件的尾部最新内容,类似于机场候机厅的大屏幕,总会把最新的消息展示给用户,对阅读日志文件尤为适合,而不需要手动刷新。
二、常用参数
可以使用tail --help命令查看所有参数
[root@mufenggrow ~]# tail --help
用法:tail [选项]... [文件]...
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 显示此帮助信息并退出
--version 显示版本信息并退出
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.
如果您希望即时追查一个文件的有效名称而非描述内容(例如循环日志),默认
的程序动作并不如您所愿。在这种场合可以使用--follow=name 选项,它会使
tail 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。
三. tail 使用案例示范
3.1 输出后10行
[root@mufenggrow ~]# tail /etc/passwd |wc -l
10
3.2 输出最后100个字符
[root@mufenggrow ~]# tail -c 100 /etc/passwd
gin
laoxin:x:1000:1000:laoxin:/home/laoxin:/bin/bash
itlaoxin:x:1001:1001::/home/itlaoxin:/bin/bash
[root@mufenggrow ~]#
3.3 从第100个字符开始输出,一直到最后
[root@mufenggrow ~]# tail -c +100 /etc/passwd
login
adm:x:3:4:adm:/var/adm:/sbin/nologin
3.4 从第43行开始一直到最后
[root@mufenggrow ~]# tail -n +43 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
3.5 输出文件的最后十行并监控文件内容的变化
这里就可以使用tail -f动态的监控日志的变化。
四. tailf, tail -f , tail -Fd 区别
-
tail -f
等同于–follow=descriptor,
常用于日志内容的跟踪,根据文件描述符进行追踪,当文件改名或被删除,追踪停止 -
tail -F
等同于–follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪 -
tailf
等同于tail -f -n 10
与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件。
最后:
tailf特别适合那些便携机上跟踪日志文件,因为它减少了磁盘访问,节省了资源。
当我们设置了滚动日志时,需要持续实时监控最新的日志输出,那么就要用tail -F,而不能用tailf 和 tail -f。
滚动日志达到一定的阈值就会重新命名,这三个命令中只有tail -F可以在重新命名后继续追踪。
总结
以上就是对tail命令的总结,linux基础命令150个,已经更新到第12个,继续加油。
相关文章
- linux添加静态路由命令_linux route add永久路由
- Linux 下端口指令妙用介绍(linux端口命令是什么)
- Linux命令:改变文件权限(linux命令修改权限)
- 命令Linux下Echo命令的使用方法(linux下echo)
- 探究Linux中的lsof命令(linux命令lsof)
- 深入探索Linux命令行模式(linux命令模式)
- 掌握Linux:配置中文语言环境(linux语言设置中文)
- 每日一Linux命令:让Linux操作更轻松(每日一linux命令)
- Linux 下添加路由命令指南(linux添加路由命令)
- 系统安装失败虚拟机遭遇Linux系统安装失败!(虚拟机中linux)
- 、1>>和0>>的区别Linux 中2>>、1>>和0>>的区别(linux中2)
- Linux下建立目录的命令:mkdir(linux建立目录命令)
- Linux之旅:探索开源世界的分支(linux的分支)
- Linux下如何输入命令指南(linux如何输入命令)
- Linux解压工具:快速、稳定的解压之旅(linux解压器下载)
- Linux命令sync的使用指南(linux命令sync)
- 探索Linux的强大分支(linux的分支)
- Linux系统的繁荣之路:各种分支的发展史(linux的分支)
- 掌握Linux文件系统格式化技巧,轻松管理数据存储(linux格式化文件系统)
- 学习 Linux 终端命令,轻松玩转操作系统(linux终端命令)
- 命令最简单Linux命令: 快速入门指南(最简单linux)
- 如何使用Linux命令安装jar文件(linux安装jar命令)
- Linux命令大变革!一文掌握最新交互式Shell和快捷操作技巧(linux改变命令)
- 韩顺平:开启Linux之旅(韩顺平 linux)
- 掌握 Linux 打印机命令,轻松搞定打印问题(linux打印机命令)
- “深入理解:使用Linux命令时的相关参数”(linux命令带参数)
- Linux命令 sort帮助你整理文件(linux命令 sort)
- Linux 下搜索文件的必备命令(linux 搜索文件命令)
- Linux 下添加用户的命令指南(linux 添加用户命令)