#Bash 的 tail 命令
1tail [OPTION]... [FILE]... 2
功能
查看文件的末尾。
类型
可执行文件(/usr/bin/tail),属于 coreutils。
参数
OPTION选项:-c,--bytes=[+]NUM- 查看文件末尾的NUM字节,如果使用正号,则表示查看第NUM字节之后的内容-f,--follow[={name|descriptor}]- 跟随文件,当文件内容增加时打印追加的内容-F- 等价于--follow=name --retry-n,--lines=[+]NUM- 查看文件末尾的NUM行,如果使用正号,则表示查看第NUM行之后的内容--max-unchanged-stats=N- 使用-f选项选项时,如果连续N次读取文件不变,则尝试重新打开文件--pid=PID- 使用-f选项选项时,当PID进程结束时停止此命令-q,--quiet,--silent- 不打印包含文件名在内的头部信息--retry- 如果文件无法打开,则重新尝试打开-v,--verbose- 打印包含文件名在内的头部信息-z,--zero-terminated- 以空字符(\0)作为行的结尾,而不是换行符(\n)--help- 显示帮助--version- 显示版本
FILE- 文件列表,如果没有这个参数或指定为-,则读取标准输入
#示例
基本功能
1$ man tail | tail -n 5 # 查看末尾 5 行 2 3 Full documentation <https://www.gnu.org/software/coreutils/tail> 4 or available locally via: info '(coreutils) tail invocation' 5 6GNU coreutils 9.4 April 2024 TAIL(1) 7$ man tail | tail -n +90 # 显示第 90 行之后的内容 8 There is NO WARRANTY, to the extent permitted by law. 9 10SEE ALSO 11 head(1) 12 13 Full documentation <https://www.gnu.org/software/coreutils/tail> 14 or available locally via: info '(coreutils) tail invocation' 15 16GNU coreutils 9.4 April 2024 TAIL(1) 17
监听日志
1$ tail -f /var/log/syslog # 监听该文件,文件内容更新时打印追加的内容 22025-11-15T08:30:00.190259+00:00 dev systemd[1]: Finished sysstat-collect.service - system activity accounting tool. 32025-11-15T08:35:01.817178+00:00 dev CRON[3239701]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) 42025-11-15T08:40:00.422316+00:00 dev systemd[1]: Starting sysstat-collect.service - system activity accounting tool... 52025-11-15T08:40:00.435212+00:00 dev systemd[1]: sysstat-collect.service: Deactivated successfully. 62025-11-15T08:40:00.435761+00:00 dev systemd[1]: Finished sysstat-collect.service - system activity accounting tool. 72025-11-15T08:45:01.832707+00:00 dev CRON[3241555]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) 82025-11-15T08:50:00.422442+00:00 dev systemd[1]: Starting sysstat-collect.service - system activity accounting tool... 92025-11-15T08:50:00.434650+00:00 dev systemd[1]: sysstat-collect.service: Deactivated successfully. 102025-11-15T08:50:00.435219+00:00 dev systemd[1]: Finished sysstat-collect.service - system activity accounting tool. 112025-11-15T08:55:01.848254+00:00 dev CRON[3243092]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) 12 13
#相关命令
| 命令 | 说明 |
|---|---|
| head | 查看文件开头 |
| more | 分页查看文件内容 |
| less | 增强版文件分页阅读命令 |
#推荐阅读
#手册
显示
1TAIL(1) User Commands TAIL(1) 2 3NAME 4 tail - output the last part of files 5 6SYNOPSIS 7 tail [OPTION]... [FILE]... 8 9DESCRIPTION 10 Print the last 10 lines of each FILE to standard output. With more 11 than one FILE, precede each with a header giving the file name. 12 13 With no FILE, or when FILE is -, read standard input. 14 15 Mandatory arguments to long options are mandatory for short options 16 too. 17 18 -c, --bytes=[+]NUM 19 output the last NUM bytes; or use -c +NUM to output starting 20 with byte NUM of each file 21 22 -f, --follow[={name|descriptor}] 23 output appended data as the file grows; 24 25 an absent option argument means 'descriptor' 26 27 -F same as --follow=name --retry 28 29 -n, --lines=[+]NUM 30 output the last NUM lines, instead of the last 10; or use -n 31 +NUM to skip NUM-1 lines at the start 32 33 --max-unchanged-stats=N 34 with --follow=name, reopen a FILE which has not 35 36 changed size after N (default 5) iterations to see if it has 37 been unlinked or renamed (this is the usual case of rotated log 38 files); with inotify, this option is rarely useful 39 40 --pid=PID 41 with -f, terminate after process ID, PID dies 42 43 -q, --quiet, --silent 44 never output headers giving file names 45 46 --retry 47 keep trying to open a file if it is inaccessible 48 49 -s, --sleep-interval=N 50 with -f, sleep for approximately N seconds (default 1.0) between 51 iterations; with inotify and --pid=P, check process P at least 52 once every N seconds 53 54 -v, --verbose 55 always output headers giving file names 56 57 -z, --zero-terminated 58 line delimiter is NUL, not newline 59 60 --help display this help and exit 61 62 --version 63 output version information and exit 64 65 NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, 66 M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, 67 E, Z, Y, R, Q. Binary prefixes can be used, too: KiB=K, MiB=M, and so 68 on. 69 70 With --follow (-f), tail defaults to following the file descriptor, 71 which means that even if a tail'ed file is renamed, tail will continue 72 to track its end. This default behavior is not desirable when you re‐ 73 ally want to track the actual name of the file, not the file descriptor 74 (e.g., log rotation). Use --follow=name in that case. That causes 75 tail to track the named file in a way that accommodates renaming, re‐ 76 moval and creation. 77 78AUTHOR 79 Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Mey‐ 80 ering. 81 82REPORTING BUGS 83 GNU coreutils online help: <https://www.gnu.org/software/coreutils/> 84 Report any translation bugs to <https://translationproject.org/team/> 85 86COPYRIGHT 87 Copyright © 2023 Free Software Foundation, Inc. License GPLv3+: GNU 88 GPL version 3 or later <https://gnu.org/licenses/gpl.html>. 89 This is free software: you are free to change and redistribute it. 90 There is NO WARRANTY, to the extent permitted by law. 91 92SEE ALSO 93 head(1) 94 95 Full documentation <https://www.gnu.org/software/coreutils/tail> 96 or available locally via: info '(coreutils) tail invocation' 97 98GNU coreutils 9.4 April 2024 TAIL(1) 99
《Bash 的 tail 命令》 是转载文章,点击查看原文。