1. tail 命令说明
tail:用来查看文件的最后几行内容(默认10行),还可以查看持续更新的文件,用法基本与 head 命令一样,参数也大多相同,基本信息如下:
NAME
tail - output the last part of files
SYNOPSIS
tail [OPTION]... [FILE]...
DESCRIPTION
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=K
output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent
-F same as --follow=name --retry
-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
TAIL(1) User Commands TAIL(1)
NAME
tail - output the last part of files
SYNOPSIS
tail [OPTION]... [FILE]...
DESCRIPTION
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=K
output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent
-F same as --follow=name --retry
-n, --lines=K
output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth
--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 even when it is or becomes inaccessible; useful when following by name, i.e., with --follow=name
-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
--help display this help and exit
--version
output version information and exit
If the first character of K (the number of bytes or lines) is a ‘+’, print beginning with the Kth item from the start of each file,
otherwise, print the last K items in the file. K may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB
1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
With --follow (-f), tail defaults to following the file descriptor, which means that even if a tail’ed file is renamed, tail will con-
tinue to track its end. This default behavior is not desirable when you really want to track the actual name of the file, not the
file descriptor (e.g., log rotation). Use --follow=name in that case. That causes tail to track the named file in a way that accom-
modates renaming, removal and creation.
部分参数如下:
选项 | 作用 |
---|---|
-c | 显示最后 K 字节的内容,或者用 -c +K显示从第 K 个字节开始的文件内容 |
-f | 显示文件尾部的内容,不断刷新 |
-n | 显示最后 K 行内容或用 -n +K 显示从第 K 行开始的文件内容 |
-q | 不显示文件名 |
-s | 使用 -f, 每次迭代之间停顿大约 N 秒(默认1秒) 使用 inotify and --pid=P 时,每隔 N 秒至少检查一次 P |
-v | 显示文件名 |
2. tail 命令语法
tail [OPTION]... [FILE]...
3. tail 命令示例
3.1 -c(按照字节)
-c 按照后面数值,显示字节,空格、换行符都算1个字节。数值可以带 + 或者 -,带 + 表示从第 num 个字节开始,显示剩余的全部内容。
tail -c num fileName
3.2 -n(按照行数)
与 head 命令一样,-n 也是按照行数显示文件内容,只不过是显示文件末尾内容。
简写的方式是直接写 -数值。
举例:显示文件最后两行
tail -n 2 fileName
tail -2 fileName
3.3 -f(持续显示)
对于持续写入的文件,例如日志文件,可以使用 -f 持续显示写入的新内容。
tail -f fileName
默认一开始显示10行,也可以修改这个行数,例如修改为一开始3行显示:
tail -3f fileName
不管一开始几行,都会持续显示后面写入的内容。
使用 Ctrl+c 可退出 tail -f。
4. 总结
tail 用于查看大文件最后几行内容,可以使用 -f 持续显示写入的内容。