回到手册索引

命令用途

tail 命令用于显示文件的末尾内容(默认显示最后10行),常用于实时监控日志文件更新。

常用用法示例

  1. 显示文件末尾默认10行

    1
    2
    3
    4
    5
    tail file.log
    [Line 91] 2023-10-01 12:00:00 INFO Service started
    [Line 92] 2023-10-01 12:00:05 DEBUG Processing request
    ...
    [Line 100] 2023-10-01 12:00:30 INFO Connection closed

    输出 file.log 的最后10行内容。

  2. 指定显示末尾行数

    1
    2
    3
    4
    tail -n 5 file.log
    [Line 96] 2023-10-01 12:00:25 WARN Low memory
    ...
    [Line 100] 2023-10-01 12:00:30 INFO Connection closed

    仅显示文件最后5行。

  3. 实时跟踪文件更新

    1
    2
    tail -f access.log
    (持续输出新增内容,直到按 Ctrl+C 终止)

    监控文件实时变化,常用于查看日志追加内容。

  4. 跟踪文件并显示文件名

    1
    2
    3
    tail -f -v server.log
    ==> server.log <==
    [New Line] 2023-10-01 12:01:00 INFO New request

    -v 参数会在输出时显示被监控的文件名。

  5. 同时跟踪多个文件

    1
    2
    3
    4
    5
    6
    tail -f app.log error.log
    ==> app.log <==
    [New log]...

    ==> error.log <==
    [Error]...

    实时监控多个文件的更新内容,并标注来源文件。

  6. 静默模式显示多个文件

    1
    2
    3
    4
    5
    6
    7
    8
    tail -q -n 3 file1.txt file2.txt
    File1 Line3
    File1 Line2
    File1 Line1

    File2 Line3
    File2 Line2
    File2 Line1

    -q 参数隐藏文件名标题,仅输出内容。

  7. 从第N行开始显示到末尾

    1
    2
    3
    4
    tail -n +20 data.csv
    Row20,Value1
    Row21,Value2
    ...

    显示从第20行开始到文件末尾的所有内容。

  8. 显示文件末尾字节数

    1
    2
    tail -c 100 binary.dat
    (输出二进制文件的最后100字节内容)

    -c 参数按字节数而非行数截取内容。

常用参数选项

  • -n, –lines=N
    指定显示末尾N行(例如 -n 20),若为+N则从第N行开始显示到末尾。
  • -f, –follow
    实时跟踪文件更新(常用于监控日志)。
  • –pid=PID
    与 -f 结合使用,当指定进程ID终止后自动停止跟踪。
  • -s, –sleep-interval=S
    设置监控间隔时间(单位:秒),例如 -f -s 5 每5秒检查一次更新。
  • -v, –verbose
    始终显示文件名标题(多文件操作时默认显示,单文件需显式指定)。
  • -q, –quiet
    静默模式,隐藏文件名标题。
  • -c, –bytes=N
    按字节数截取内容(例如 -c 500 显示最后500字节)。
  • -F
    类似 -f,但会跟踪文件重命名或轮转(适用于日志切割场景)。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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=[+]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; can be
repeated to watch multiple processes

-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

NUM 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, R, Q. Binary prefixes can be used, too:
KiB=K, MiB=M, and so on.

With --follow (-f), tail defaults to following the file
descriptor, which means that even if a tail'ed file is renamed,
tail will continue 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 accommodates renaming, removal and creation.

AUTHOR

Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim
Meyering.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

head(1)

Full documentation <https://www.gnu.org/software/coreutils/tail>
or available locally via: info '(coreutils) tail invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/⟩ on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org