关于 tail 命令的几个实用例子

系统 Linux
前一篇文章我们介绍了 head 命令的使用,今天我们在来介绍一下 tail。

顾名思义,tail 命令输出单个或多个文件的最后部分内容。默认情况下,tail 命令将会打印文件的最后 10 行内容。在实际应用中,我们经常用它来实时读取日志文件。

tail 命令的语法结构如下所示:

tail [options] [files]

作为演示,我们使用如下文件内容来介绍 tail 命令:​

The Mysterious Affair at Styles
The Secret Adversary
The Murder on the Links
The Man in the Brown Suit
The Secret of Chimneys
The Murder of Roger Ackroyd
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

刚刚我们提到过,tail 命令默认会显示文件的最后 10 行内容,但是当文件内容总共不足 10 行时,会显示整个文件的内容。当然大多数情况下,我们不使用它的默认行为,而是根据实际情况来选择应用。接下来我们根据例子来逐个介绍下。

1,使用 tail 命令打印文件的最后 x 行内容

打印文件的最后 x 行内容(而不是默认的最后 10 行),可参考如下语法结构:

tail -n x <filename>

比如,查看文件的最后 5 行内容:​

$ tail -n 5 tiap.txt
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

小提示:你也可以直接使用 tail -x 而不是 tail -n x 来显示最后 x 行内容。

2,打印从 x 行开始往后的所有内容

如果想要打印从 x 行开始往后的所有内容,可使用 +x 选项,如下所示:

tail -n +x <filename>

比如,我们的示例文档,想要打印从第 7 行开始的所有内容,如下:

$ tail -n +7 tiap.txt
The Big Four
The Mystery of the Blue Train
The Seven Dials Mystery
The Murder at the Vicarage
Giant's Bread
The Floating Admiral
The Sittaford Mystery
Peril at End House
Lord Edgware Dies
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

3,使用 tail 命令同时查看多个文件

使用 tail 命令可以同时查看多个文件。其语法结构如下所示:

tail -n N <file1> <file2> <file3>

与 head 命令类似,查看的各个文件名会显示在输出中。比如,我们想要查看 tiap.txt 和  sherlock.txt 的最后 3 行,如下所示:

$ tail -n3 sherlock.txt tiap.txt
==> sherlock.txt <==
The Adventure of the Noble Bachelor
The Adventure of the Beryl Coronet
The Adventure of the Copper Beeches
==> tiap.txt <==
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds

提示,使用 -q 选项可以在输出中不显示文件名信息。

4,使用 tail 命令实时监视文件

假设我们有一个文件,其内容会实时增加(比如日志文件),tail 命令可以帮助我们实时查看新添加到文件中的内容。这个功能可通过使用 -f 选项来实现:

tail -f <log-file>

上述命令会首先显示文件的最后 10 行内容,然后当文件有新的内容增加时,也会将新的内容实时输出到终端中。这个功能被广泛用于读取日志文件,这也可能是 tail 命令最常见的用法。

提示:如果使用 -F 选项代替 -f 选项,tail 命令会等待输入文件创建(如果不存在)后,在实时显示文件的内容。

5,在管道重定向中使用 tail 命令

tail 命令还可以结合管道重定向​来使用。比如,假如在一个目录中有许多文件,我们只想查看最后 3 个修改过的文件,那么可以按如下方式:

ls -ltr | tail -n3

上述命令中,ls -lrt 按时间顺序倒叙列出所有文件,然后通过管道重定向将输出给到 tail 命令,tail 命令解析这个输出,只显示其最后的 3 行,这 3 行内容就是我们所需要的最近 3 个修改的文件列表。

6,在 tail 命令的输出中显示行号

我们在查看某个文档的时候,如果文档中可以显示行号,那么对于我们来说是非常友好的,比如我们想要查看当前正在阅读的在多少行,整个文件有多少行内容,等等。

不过,tail 命令并没有内置的显示行号的功能。不过我们可以通过其他方式来实现这一功能,那就是通过使用 nl 命令以及管道重定向结合 tail 命令来实现。

nl 命令可以在文件内容中显示行号,将其输出通过管道重定向到 tail 命令中,就可以满足我们的需求。如下所示:

nl <filename> | tail -3
责任编辑:庞桂玉 来源: TIAP
相关推荐

2023-03-23 21:08:59

head命令

2014-03-17 17:27:51

Linux mvLinux 命令

2023-02-02 14:06:00

history命令技巧

2016-09-26 14:40:25

Windows内网实用命令

2022-11-02 08:32:46

find 命令Linux

2020-07-22 13:50:39

shell命令前端

2018-08-03 11:07:52

dd命令备份Linux系统

2022-11-09 19:02:10

Linux

2020-02-17 11:54:18

网络路由器命令

2023-05-04 12:39:27

GDB命令程序

2017-04-10 18:45:47

2009-10-13 14:33:00

2015-07-14 10:13:29

2018-02-25 10:45:08

Linux命令uptime

2018-02-24 14:00:42

TensorFlow数学计算机器学习

2010-07-14 16:09:52

Telnet命令例子

2023-04-20 13:59:01

Pythonwhile循环的

2022-05-07 09:30:08

watchtailLinux 系统

2024-01-03 15:35:56

Linux工具Tail命令

2017-03-13 16:48:05

Git技巧
点赞
收藏

51CTO技术栈公众号