Cat 命令是 Linux 中显示文件内容最常用的命令之一。 但是您可能并不总是希望显示一个大文件的所有行。 在这种情况下,tail 命令会有所帮助。
什么是尾巴命令?
这 尾部命令,顾名思义,输出单个文件或多个文件的最后部分。 默认情况下,tail 命令打印输入文件的最后十行。 tail 命令还用于实时读取日志文件。
tail 命令的语法是:
tail [options] [files]
Linux中Tail命令的5个实例
让我们通过一些实际的例子来学习如何在 Linux 中使用 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
你可以 从我们的 GitHub 存储库下载示例文件 并在练习的同时按照教程进行操作。
如果您使用不带任何选项的 tail 命令,它将打印最后 10 行。 这是 tail 命令的默认行为。
tail agatha.txt
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 命令的默认用法。 还有更多。 让我们一一看看tail命令最常见的用法。
1.用tail命令打印最后N行
要查看最后 N 行,而不是默认的 10 行,可以按以下方式使用 tail 命令:
tail -n N <filename>
为了 example如果你想在我们的 tail 命令中看到最后 5 行 example 文件,你可以像这样使用它:
tail -n 5 agatha.txt
Murder on the Orient Express
Unfinished Portrait
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds
提示:您也可以简单地使用 tail -N 而不是 tail -n N 来显示文件的最后 N 行。
2.打印以行号N开头的所有行
如果要查看从第 N 行开始的所有行,可以在此处使用 + 选项。
tail -n +N <filename>
在我们的示例文件中,如果您想查看从第 7 行开始的所有行,您可以按以下方式使用它:
tail -n +7 agatha.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>
所有文件的输出组合在一起,默认情况下,文件名显示在以“==>”开头的行中。
为了 example如果你想查看文件的最后三行 夏洛克.txt 和 agatha.txt,它会是这样的:
tail -n3 sherlock.txt agatha.txt
==> sherlock.txt <==
The Adventure of the Noble Bachelor
The Adventure of the Beryl Coronet
The Adventure of the Copper Beeches
==> agatha.txt <==
Why Didn't They Ask Evans?
Three Act Tragedy
Death in the Clouds
提示:您可以使用带有选项 -q 的安静模式从输出中删除文件名。
4、使用tail命令实时监控文件 [Very useful for log monitoring]
假设您有一个文件并且向其中添加了新内容。 tail 命令允许您在添加到文件时显示所有新行。
为此,您可以使用 -f 选项。
tail -f <log-file>
该命令将首先显示文件的最后 10 行,然后在将新行添加到文件时更新输出。
这广泛用于实时查看日志文件。 这可能是 tail 命令最实际的用途。
提示:如果使用 -F 而不是 -f 选项,tail 命令将等待输入文件被创建(如果它不存在),然后实时显示文件的内容。
5. 对管道使用tail命令
tail 命令可以与其他使用管道的命令结合使用。
为了 example,如果您的目录中有太多文件,并且只想查看最后 3 个修改的文件,则可以按以下方式使用它:
ls -ltr | tail -n3
在上述命令中, ls -lrt 按时间倒序列出所有文件。 然后tail命令进一步解析这个输出,只显示ls命令输出的最后三行,因此我们得到最后三个修改文件的列表。
额外提示:显示带有行号的尾部命令输出
行号有助于理解和分析输出。 假设您显示了文件的最后 20 行,但您还想查看它们的行号,以便查看文件中的总行数。
不幸的是,没有内置选项来显示带有数字的尾部命令输出。
但这并不意味着您不能使用 tail 命令显示行号。 为此,您可以使用管道的力量。 您刚刚在上一节中看到 tail 命令可以与管道一起使用。 为什么不在这里使用它。
nl 是用行号显示文件内容的方式。 如果使用管道将它与 tail 命令结合使用,则可以显示 tail 命令的输出和行号。
nl <filename> | tail -3
我希望您发现 tail 命令示例对您有所帮助。 我也建议阅读有关 head 命令的内容。 如果您有任何建议或问题,请在下面的评论部分分享。