linux基本功系列之tail命令实战

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 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个,继续加油。

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
11天前
|
Linux Shell
Linux 中 Tail 命令的 9 个实用示例
Linux 中 Tail 命令的 9 个实用示例
40 6
Linux 中 Tail 命令的 9 个实用示例
|
7天前
|
设计模式 Java Linux
Linux的20个常用命令
Linux的23个常用命令
Linux的20个常用命令
|
2天前
|
机器学习/深度学习 存储 Linux
linux中强大且常用命令:find、xargs、grep
linux中强大且常用命令:find、xargs、grep
25 9
|
2天前
|
SQL 移动开发 Linux
linux下find、grep命令详解
linux下find、grep命令详解
37 8
|
9天前
|
存储 Linux 编译器
linux中vim介绍以及常用命令大全
linux中vim介绍以及常用命令大全
32 8
|
12天前
|
机器学习/深度学习 安全 网络协议
Web安全-Linux网络命令
Web安全-Linux网络命令
14 1
|
Linux Shell Unix
带你读《Linux实战》之一:欢迎使用Linux
你正在期望学习管理Linux计算机吗?这是一个很好的选择。虽然Linux常常驻留于消费者的桌面计算机上,但它同时也是服务器领域的绝对主宰,特别是虚拟服务器和云服务器。如果你打算管理当前引人关注的服务器和网络体系架构,你将不得不围绕Linux的命令行展开学习。除了第1章之外,本书的每一章都包括一个或两个实际项目。鉴于第1章的内容主要用来填补你的Linux知识体系中可能存在的基础知识空白,因此其组织形式与其他章节有所不同。
|
22天前
|
Linux 网络安全 Python
linux后台运行命令screen的使用
linux后台运行命令screen的使用
56 2
linux后台运行命令screen的使用
|
22天前
|
Ubuntu Linux
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
144 3
|
18天前
|
机器学习/深度学习 安全 网络协议
Linux防火墙iptables命令管理入门
本文介绍了关于Linux防火墙iptables命令管理入门的教程,涵盖了iptables的基本概念、语法格式、常用参数、基础查询操作以及链和规则管理等内容。
178 73