Linux tail命令教程

Linux has a lot of tools to display file content. cat, less, tail, etc. are some of them. They have different features and usage areas. The tail is mainly developed and used to display the end of the file. We will look at different features and usage examples of the tail command.

Linux有很多工具来显示文件内容。 猫,少,尾巴等等。 它们具有不同的功能和使用区域。 尾巴主要用于显示文件的结尾。 我们将看看tail命令的不同功能和用法示例。

tail命令语法 (tail Command Syntax)

We will use following syntax for tail command.

我们将对tail命令使用以下语法。

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

tail命令帮助 (tail Command Help)

We can get help information about the tail command by providing the option --help.

我们可以通过提供--help选项来获取有关tail命令的帮助信息。

$ tail --help
Help
Help
帮帮我

使用tail命令打印最后10行(Print Last 10 Lines with tail Command)

We will start simply tailing a file. We will use one of the log files which resides in /var/log directory. We will tail auth.log file which provides authentication-related logs. Tail command by default prints the last 10 lines of the provided file and quit.

我们将简单地尾随一个文件。 我们将使用/var/log目录中的一个日志文件。 我们将尾部的auth.log文件提供与身份验证相关的日志。 默认情况下,Tail命令将打印提供的文件的最后10行并退出。

$ tail /var/log/auth.log
Tail
Tail
尾巴

指定要显示的行数(Specify Count Of Lines To Display)

In previous example we have not provided any option or parameter to specify the number of lines to display from end of file. So tail only display last 10 lines by default. But we can specify the count of lines with -n option. In this example we will show last 25 lines of the auth.log file.

在前面的示例中,我们没有提供任何选项或参数来指定要从文件末尾显示的行数。 因此,默认情况下,tail仅显示最后10行。 但是我们可以使用-n选项指定行数。 在此示例中,我们将显示auth.log文件的最后25行。

$ tail -n 25 /var/log/auth.log
Specify Count Of Lines To Display
Specify Count Of Lines To Display
指定要显示的行数

指定要显示的字节数(Specify Count Of Bytes To Display)

Up to now in the examples, we have specified the line count but there is another type of metric to display file tails. We can specify the count of bytes to display from the end of the file. We will use -c parameter to specify byte count. In this example, we will show only 200 bytes from the end of auth.log file.

到目前为止,在示例中,我们已经指定了行数,但是还有另一种度量标准来显示文件尾部。 我们可以指定从文件末尾开始显示的字节数。 我们将使用-c参数指定字节数。 在此示例中,我们将仅显示auth.log文件末尾的200个字节。

$ tail -c 200 /var/log/auth.log
Specify Count Of Bytes To Display
Specify Count Of Bytes To Display
指定要显示的字节数

As we can the the output is very less because each character is counted as one byte.

尽我们所能,输出将非常少,因为每个字符都被计为一个字节。

LEARN MORE  Logrotate Command Tutorial With Examples For Linux
了解更多Logrotate命令教程,以及适用于Linux的示例

使用tail命令显示多个文件(Display Multiple Files with tail Command)

In some situations, we may need to display multiple files in a single tail command. There is already this feature in tail command. Just add other files at the end of the tail command. In this example, we will list files auth.log , syslog and dpkg.log .

在某些情况下,我们可能需要在单个tail命令中显示多个文件。 tail命令中已经具有此功能。 只需在tail命令的末尾添加其他文件即可。 在此示例中,我们将列出文件auth.logsyslogdpkg.log

$ tail -n 25 /var/log/auth.log /var/log/syslog /var/log/dpkg.log
Display Multiple Files
Display Multiple Files
显示多个文件

As we can see each file content is separated with delimiters and file name like below.

如我们所见,每个文件内容都由分隔符和文件名分隔,如下所示。

==> /var/log/syslog <==

以交互方式显示新添加的行或使用tail命令跟随 (Display Newly Added Lines Interactively or Follow with tail Command)

One of the most used features of the tail command is the following files. This feature is generally used for log files for debugging and troubleshooting. With this option, the tail will not quit after printing the file end. It will wait for more content which will be added to the file in real-time. If some more content is added to the file the content will be displayed with the tail. In this example, we wait new logs for auth.log file.

tail命令最常用的功能之一是以下文件。 此功能通常用于日志文件以进行调试和故障排除。 使用此选项,在打印文件末尾后尾部不会退出。 它将等待更多内容,这些内容将实时添加到文件中。 如果将更多内容添加到文件,则内容将显示在尾部。 在此示例中,我们等待新日志获取auth.log文件。

$ tail -f /var/log/auth.log
Display Newly Added Lines or Follow
Display Newly Added Lines or Follow
显示新添加的行或关注

We login to the system ubu2 at 07:21:59 and new logs are created and added to the auth.log file. This logs are displayed by tails automatically in real time. Tail tracks the auth.log file for changes.

我们在07:21:59登录到ubu2系统,并创建了新日志并将其添加到auth.log文件中。 该日志由尾部自动实时实时显示。 尾部跟踪auth.log文件中的更改。

用tail命令过滤线 (Filter Lines with tail Command)

In some situations we may have a lot of logs to look but searching only some text in the logs. We need some filtering mechanism to look at the end of file. Here we will use external tool grep to filter logs. We will filter only lines those contains  text  ismail  int the last 10 lines of auth.log

在某些情况下,我们可能需要查看许多日志,但仅在日志中搜索一些文本。 我们需要一些过滤机制来查看文件末尾。 在这里,我们将使用外部工具grep过滤日志。 我们将仅过滤那些包含文本ismail int的auth.log的最后10行的行

$ tail  /var/log/auth.log | grep ismail
Filter Lines
Filter Lines
过滤线

指定间隔值以使用tail命令跟随文件(Specify Interval Value To Follow File with tail Command)

Following or tracking is done in real-time. Real-time means whenever some text appended to the end of file this text will be printed by the tail. If there is a lot of input and reading this input is affecting performance of the system we need a more relaxing option. Here we can specify the interval for the following file. Added lines will be printed not real-time. They will be printed in the specified interval. In the following example, we will use 2 seconds as the interval value.

跟踪或跟踪是实时完成的。 实时表示每当文件末尾附加一些文本时,该文本将在尾部打印。 如果输入很多,那么读取此输入会影响系统的性能,我们需要一个更轻松的选择。 在这里,我们可以指定以下文件的间隔。 添加的行将不会实时打印。 它们将以指定的间隔打印。 在下面的示例中,我们将2秒用作间隔值。

$ tail -s 2 -f /var/log/auth.log
Specify Sleep Time While Following
Specify Sleep Time While Following
在跟踪时指定睡眠时间

指定读取重试计数(Specify Read Retry Count)

While reading an input some times problems will occur which can prevent the reading of files. If a problem-related reading occurs the tail will exit by default. We can change this behavior by setting some retry. These options can be set with a --retry option. In the example, we will read auth.log with retry option.

在读取输入内容时,有时会出现一些问题,这些问题会阻止文件的读取。 如果出现与问题相关的读数,则尾巴将默认退出。 我们可以通过设置一些重试来更改此行为。 可以使用--retry选项设置这些选项。 在示例中,我们将使用重试选项读取auth.log

$ tail --retry -f /var/log/auth.log
Specify Try To Read Count
Specify Try To Read Count
指定尝试读取计数

从交互模式退出(Quit From Interactive Mode)

Without following a file tail will automatically exit. But if tail follows a file it will work forever. If we want to exit and kill tail CTRL+c shortcut can be used in a console.

不遵循文件尾部将自动退出。 但是,如果tail跟随文件,它将永远有效。 如果我们想退出并杀死尾巴,可以在控制台中使用CTRL+c快捷键。

CTRL+c
Quit Tail
Quit Tail
退出尾巴

删除标题行(Remove Header Line)

Previously we have looked to tailing multiple files. While using multiple files there will be some delimiter used to set the start of the separate file also provide the name of the file. This header line can be removed with -q option.

以前,我们希望拖尾多个文件。 在使用多个文件时,将使用一些分隔符来设置单独文件的开始,还提供文件名。 可以使用-q选项删除该标题行。

$ tail -n 5 -q /var/log/auth.log /var/log/syslog /var/log/dpkg.log
Remove Header
Remove Header
删除标题

翻译自: https://www.poftut.com/linux-tail-command-tutorial-examples/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值