引言
我们作为 Linux 用户,经常会操作那些在后台长时间运行的进程,这些进程被称作守护进程或服务。例如 Secure Shell (sshd)、Network Manager (networkd)、Volume Manager (LVM)、Cron 等都是服务的典型例子,这样的服务还有很多。
在许多情况下,我们需要查看这些服务的日志文件来解决系统问题。但问题在于,这些服务会产生大量的日志信息,逐条查看这些日志往往既费时又费力。这时,tail 命令就派上用场了。
tail 命令是一个命令行工具,它与 head 命令类似,用于读取文件内容,并将一个或多个文件的最后 10 行显示在屏幕上。
在这篇[1]实用的教程中,我们将深入了解 tail 命令。教程结束后,Linux 命令行用户将能够熟练地运用 tail 命令。
1.在Linux中打印文件的最后10行
默认情况下,tail 命令打印给定文件的最后 10 行,如图所示。
$ tail /var/log/secure
Apr 2 14:17:24 TecMint sshd[201178]: Disconnected from user tecmint 192.168.0.162 port 59774
Apr 2 14:17:24 TecMint sshd[201165]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 14:29:12 TecMint sshd[201366]: Accepted password for tecmint from 192.168.0.162 port 56378 ssh2
Apr 2 14:29:12 TecMint systemd[201371]: pam_unix(systemd-user:session): session opened for user tecmint(uid=1002) by (uid=0)
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session opened for user tecmint(uid=1002) by (uid=0)
Apr 2 14:29:12 TecMint sshd[201382]: Received disconnect from 192.168.0.162 port 56378:11: disconnected by user
Apr 2 14:29:12 TecMint sshd[201382]: Disconnected from user tecmint 192.168.0.162 port 56378
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
在这里,我们可以看到上面的命令显示了 /var/log/secure 文件的最后十行。
2. 在 Linux 中打印文件的最后 N 行
在最后一个示例中,该命令打印给定文件的最后 10 行。但是,我们可以使用 -n 选项,它允许我们限制要在屏幕上打印的行数,如图所示。
$ tail -n 3 /var/log/secure
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
在此示例中,我们可以看到该命令现在仅显示最后三行,而不是十行。
3. Linux 中忽略文件的前 N 行
在这里,我们可以使用加号 (+) 和 -n 选项,这允许我们控制给定文件的起点。
为了理解这一点,让我们使用 +5 值从第 5 行开始输出:
$ tail -n +5 /var/log/secure
Apr 2 14:17:24 TecMint sshd[201178]: Disconnected from user tecmint 192.168.0.162 port 59774
Apr 2 14:17:24 TecMint sshd[201165]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 14:29:12 TecMint sshd[201366]: Accepted password for tecmint from 192.168.0.162 port 56378 ssh2
Apr 2 14:29:12 TecMint systemd[201371]: pam_unix(systemd-user:session): session opened for user tecmint(uid=1002) by (uid=0)
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session opened for user tecmint(uid=1002) by (uid=0)
Apr 2 14:29:12 TecMint sshd[201382]: Received disconnect from 192.168.0.162 port 56378:11: disconnected by user
Apr 2 14:29:12 TecMint sshd[201382]: Disconnected from user tecmint 192.168.0.162 port 56378
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
4. 显示文件的最后 N 个字符
与lines类似,我们也可以使用命令使用-c选项来显示文件的最后N个字符,如下所示:
$ tail -c 7 /var/log/secure
(uid=0)
在此示例中,我们可以看到该命令显示给定文件的最后七个 ASCII 字符。
5. 删除文件的前N个字符
同样,我们可以使用加号 (+) 和 -c 选项来跳过前 N 个字符。因此,让我们使用以下命令跳过文件的第一行:
$ tail -c +5 /var/log/secure
Apr 2 03:02:59 TecMint sudo[162801]: root : TTY=pts/2 ; PWD=/root ; USER=root ; COMMAND=/bin/dnf install R
Apr 2 03:02:59 TecMint sudo[162801]: pam_unix(sudo:session): session opened for user root(uid=0) by root(uid=0)
Apr 2 03:03:02 TecMint sudo[162801]: pam_unix(sudo:session): session closed for user root
Apr 2 03:11:17 TecMint groupadd[163602]: group added to /etc/group: name=avahi, GID=70
Apr 2 03:11:18 TecMint groupadd[163602]: group added to /etc/gshadow: name=avahi
Apr 2 03:11:18 TecMint groupadd[163602]: new group: name=avahi, GID=70
Apr 2 03:11:19 TecMint useradd[163610]: new user: name=avahi, UID=70, GID=70, home=/var/run/avahi-daemon, shell=/sbin/nologin, from=none
Apr 2 03:13:41 TecMint groupadd[163704]: group added to /etc/group: name=colord, GID=986
Apr 2 03:13:41 TecMint groupadd[163704]: group added to /etc/gshadow: name=colord
在这里,我们可以看到该命令显示了除第一行之外的所有行。
6. 在标题中显示文件名
我们可以指示 tail 命令将当前文件名显示为显示标题,这在处理多个文件时会派上用场。因此,让我们使用 -v 选项来启用显示标题:
$ tail -n 3 -v /var/log/secure
==>/var/log/secure <==
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
在上面的输出中,==> /var/log/secure <== 表示显示头。
7. 在多个文件中将文件名显示为标题
就像任何其他文件处理命令一样,我们也可以通过 tail 命令使用多个文件。在这种情况下,显示标题用于分隔文件内容。
$ tail -n 3 -v /var/log/secure /var/log/secure-20230402
==> /var/log/secure <==
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
==> /var/log/secure-20230402 <==
Mar 31 03:50:53 TecMint groupadd[156163]: new group: name=docker, GID=987
Mar 31 04:46:11 TecMint sshd[159403]: Accepted password for root from 192.168.0.162 port 46480 ssh2
Mar 31 04:46:11 TecMint sshd[159403]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
在上面的输出中,我们可以看到每个文件的显示标题。
8. 如何禁用文件中的显示标题
在前面的示例中,我们看到该命令在处理多个文件时启用显示标题。但是,我们可以使用 -q 选项抑制这种默认行为。
$ tail -q -n 3 /var/log/secure /var/log/secure-20230402
Apr 2 14:29:12 TecMint sshd[201366]: pam_unix(sshd:session): session closed for user tecmint
Apr 2 15:12:55 TecMint sshd[202049]: Accepted password for root from 192.168.0.162 port 53334 ssh2
Apr 2 15:12:55 TecMint sshd[202049]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Mar 31 03:50:53 TecMint groupadd[156163]: new group: name=docker, GID=987
Mar 31 04:46:11 TecMint sshd[159403]: Accepted password for root from 192.168.0.162 port 46480 ssh2
Mar 31 04:46:11 TecMint sshd[159403]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
在这里,我们可以看到现在该命令将一个又一个地显示文件内容,并且没有任何显示头。
9. 如何观察文件的更改
到目前为止,我们看到 tail 命令在处理完所需数量的行或字符后就会退出。然而,有时我们也想查看新生成的日志。
在这种情况下,我们可以在命令中使用 -f 选项,这使我们能够实时监视文件的更改。
为了理解这一点,首先,让我们在第一个终端中执行以下命令:
$ tail -f /var/log/messages
Apr 2 15:13:28 TecMint NetworkManager[741]: [1680462808.8441] policy: set-hostname: current hostname was changed outside NetworkManager: 'TecMint'
Apr 2 15:13:28 TecMint systemd[1]: Starting Network Manager Script Dispatcher Service...
Apr 2 15:13:28 TecMint systemd[1]: Started Network Manager Script Dispatcher Service.
Apr 2 15:13:37 TecMint arpwatch[11001]: rename arp.dat -> arp.dat-: Operation not permitted
Apr 2 15:13:38 TecMint systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully.
Apr 2 15:13:58 TecMint systemd[1]: systemd-hostnamed.service: Deactivated successfully.
Apr 2 15:18:03 TecMint systemd[1]: Starting dnf makecache...
Apr 2 15:18:03 TecMint dnf[202235]: Metadata cache refreshed recently.
Apr 2 15:18:03 TecMint systemd[1]: dnf-makecache.service: Deactivated successfully.
Apr 2 15:18:03 TecMint systemd[1]: Finished dnf makecache.
在这里,我们可以看到该命令在显示最后十行后无限等待:
接下来,让我们打开另一个终端并将一些文本附加到numbers-2.txt 文件中:
$ echo "View Logs in Real-Time" >> /var/log/messages
现在,我们切换到第一个终端来查看新添加的文本:
$ tail -f /var/log/messages
Apr 2 15:13:28 TecMint NetworkManager[741]: [1680462808.8441] policy: set-hostname: current hostname was changed outside NetworkManager: 'TecMint'
Apr 2 15:13:28 TecMint systemd[1]: Starting Network Manager Script Dispatcher Service...
Apr 2 15:13:28 TecMint systemd[1]: Started Network Manager Script Dispatcher Service.
Apr 2 15:13:37 TecMint arpwatch[11001]: rename arp.dat -> arp.dat-: Operation not permitted
Apr 2 15:13:38 TecMint systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully.
Apr 2 15:13:58 TecMint systemd[1]: systemd-hostnamed.service: Deactivated successfully.
Apr 2 15:18:03 TecMint systemd[1]: Starting dnf makecache...
Apr 2 15:18:03 TecMint dnf[202235]: Metadata cache refreshed recently.
Apr 2 15:18:03 TecMint systemd[1]: dnf-makecache.service: Deactivated successfully.
Apr 2 15:18:03 TecMint systemd[1]: Finished dnf makecache.
View Logs in Real-Time
在这里,我们可以看到 tail 命令显示了新添加的文本。
Source: https://www.tecmint.com/tail-command-linux/#5_Remove_First_N_Characters_of_File
本文由 mdnice 多平台发布