Linux系统之tail命令的基本使用

在这里插入图片描述

👨‍💻 江湖有缘
🚀 一名在运维领域不懈探索的学习者。

🌟 认证与成就
🔴 红帽 RHCE 认证
🟠 华为 HCIP 数通认证
🔵 华为 HCIE 云计算认证
…以及其他多项专业认证

🌐 个人主页
🔗 CSDN个人主页

📢 公众号
🏷️ 【运维江湖客】

👥 交流社群
💬 欢迎同行及爱好者加入我们的QQ群:
🏷️ 群号【942602415】

📝 人生格言
“人可以被毁灭,但不能被打败。”
—— 海明威


一、tail命令介绍

tail命令是Linux系统中常用的命令之一,用于查看文件的末尾内容。它可以显示文件的最后几行内容,默认显示文件的末尾10行。

二、tail命令的使用帮助

2.1 tail命令的help帮助信息

使用–help,查看tail的帮助信息。

[root@openEuler-test ~]# tail --help
Usage: tail [OPTION]... [FILE]...
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=[+]NUM       output the last NUM bytes; or use -c +NUM to
                             output starting with byte NUM 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=[+]NUM       output the last NUM lines, instead of the last 10;
                             or use -n +NUM to skip NUM-1 lines at the start
      --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
  -z, --zero-terminated    line delimiter is NUL, not newline
      --help        display this help and exit
      --version     output version information and exit

2.2 tail命令的语法解释

选项长选项描述
-c--bytes=NUM输出文件尾部的 NUM 个字节内容。
-f`–follow[={namedescriptor}]`
-F等同于 --follow=name --retry,即监视文件变化并自动重试打开文件。
-n--lines=NUM输出文件的尾部 NUM 行内容。
--pid=<进程号>-f 选项连用,当指定的进程号的进程终止后,自动退出 tail 命令。
-q--quiet, --silent当有多个文件参数时,不输出各个文件名。
--retry即使在 tail 命令启动时文件不可访问或稍后变得不可访问,都始终尝试打开文件。通常与 --follow=name 连用。
-s--sleep-interval=<秒数>-f 选项连用,指定监视文件变化的时间间隔(秒数)。
-v--verbose当有多个文件参数时,总是输出各个文件名。
--help显示指令的帮助信息。
--version显示指令的版本信息。

这个表格清晰地展示了 tail 命令的各个选项及其功能描述。

NUM值后面可以有后缀:


b : 512
kB : 1000
k : 1024
MB : 1000 * 1000
M : 1024 * 1024
GB : 1000 * 1000 * 1000
G : 1024 * 1024 * 1024
T、P、E、Z、Y等以此类推。

三、tail命令的基本使用

3.1 直接使用tail命令

直接使用tail命令,默认会显示文本的后10行内容。

[root@openEuler-test ~]# tail /etc/passwd
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash

3.2 使用-n选项

-n选项,可以输出文件的尾部 NUM 行内容。

  • 显示文件的内容,从第10行至文件末尾
[root@openEuler-test ~]# tail -n +10 /etc/passwd
operator:x:11:0:operator:/root:/usr/sbin/nologin
games:x:12:100:games:/usr/games:/usr/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/usr/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/usr/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash
  • 显示文本内容,倒数10行的内容。
[root@openEuler-test ~]# tail -n 10 /etc/passwd
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash
  • 也可以使用方式,显示文本的最后10行。
[root@openEuler-test ~]# tail -10 /etc/passwd
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:D-Bus:/var/run/dbus:/sbin/nologin
polkitd:x:997:995:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin
chrony:x:995:992::/var/lib/chrony:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash

3.3 显示文件最后10个字符

  • 显示文件最后10个字符
[root@openEuler-test ~]# tail -c 10 /etc/passwd
/bin/bash

3.4 实时监控文件内容

运维经常使用的一条命令,tail -f /var/log/messages 命令用于实时监视/var/log/messages文件的内容。使用 Ctrl + C 可以停止 tail -f 命令的执行。

[root@openEuler-test ~]# tail -f /var/log/messages
Aug 13 19:43:29 openEuler systemd[1]: Starting Hostname Service...
Aug 13 19:43:29 openEuler dbus-daemon[957]: [system] Successfully activated service 'org.freedesktop.hostname1'
Aug 13 19:43:29 openEuler systemd[1]: Started Hostname Service.
Aug 13 19:43:29 openEuler sshd[8999]: Accepted password for root from 192.168.3.241 port 56708 ssh2
Aug 13 19:43:29 openEuler sshd[8999]: User child is on pid 9007
Aug 13 19:43:29 openEuler sshd[9007]: Starting session: subsystem 'sftp' for root from 192.168.3.241 port 56708 id 0
Aug 13 19:43:29 openEuler sftp-server[9051]: session opened for local user root from [192.168.3.241]
Aug 13 19:43:29 openEuler sftp-server[9051]: opendir "/root"
Aug 13 19:43:29 openEuler sftp-server[9051]: closedir "/root"
Aug 13 19:43:59 openEuler systemd[1]: systemd-hostnamed.service: Deactivated successfully.

3.5 指定变化时间隔的秒数

-s选项,一般与“-f”选项连用,指定监视文件变化时间隔的秒数。

[root@openEuler-test ~]# tail -s 10 -f /var/log/messages
Aug 13 19:43:29 openEuler systemd[1]: Starting Hostname Service...
Aug 13 19:43:29 openEuler dbus-daemon[957]: [system] Successfully activated service 'org.freedesktop.hostname1'
Aug 13 19:43:29 openEuler systemd[1]: Started Hostname Service.
Aug 13 19:43:29 openEuler sshd[8999]: Accepted password for root from 192.168.3.241 port 56708 ssh2
Aug 13 19:43:29 openEuler sshd[8999]: User child is on pid 9007
Aug 13 19:43:29 openEuler sshd[9007]: Starting session: subsystem 'sftp' for root from 192.168.3.241 port 56708 id 0
Aug 13 19:43:29 openEuler sftp-server[9051]: session opened for local user root from [192.168.3.241]
Aug 13 19:43:29 openEuler sftp-server[9051]: opendir "/root"
Aug 13 19:43:29 openEuler sftp-server[9051]: closedir "/root"
Aug 13 19:43:59 openEuler systemd[1]: systemd-hostnamed.service: Deactivated successfully.

四、注意事项

  • 当使用tail命令时,要注意指定正确的文件名或文件路径。如果文件名或路径错误,命令将无法执行成功。

  • 可以使用tail命令的选项来显示文件的最后几行,例如使用"-n"选项加上一个数字来指定要显示的行数。例如,使用"tail -n 10 file.txt"可以显示文件file.txt的最后10行。

  • 使用tail命令时,可以结合其他命令来实现更复杂的操作。例如,可以使用管道将tail命令的输出发送给其他命令,进行进一步处理。

  • tail命令默认将文件的最后10行显示在终端上。如果想要实时监控文件的变化,可以使用"-f"选项来追踪文件的变化,并将新的内容实时显示在终端上。例如,使用"tail -f file.txt"可以实时监控文件file.txt的变化。

  • 使用tail命令时,可以使用"-c"选项加上一个数字来指定要显示的字节数。例如,使用"tail -c 100 file.txt"可以显示文件file.txt的最后100个字节。

  • 可以使用tail命令的"-s"选项来指定以秒为单位的时间间隔,用于追踪文件的变化。例如,使用"tail -f -s 2 file.txt"可以每2秒显示一次文件file.txt的变化。

【原文地址:https://blog.csdn.net/jks212454/article/details/141392418】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖有缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值