每天一个linux命令14-tail

1. 命令解析

命令用途

把文件的末尾N行显示到标准输出,默认显示10行,还可通过 tail -f file 来持续输出文件的数据。

命令格式

tail [OPTION]... [FILE]...

命令参数

-c, --bytes=K output the last K bytes; or use -c +K to output
bytes starting with the Kth 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=K output the last K lines, instead of the last 10;
or use -n +K to output 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 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

2. 示例

2.1 显示文件末尾10行

[root@test headTest]# tail hisotry.log 
  213  less history.log 
  214  cd /root/test
  215  ls
  216  mkdir headTest
  217  ls
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.2 显示文件末尾5行 -n

[root@test headTest]# tail -5 hisotry.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

2.3 实时跟踪文件(默认1秒刷新一次) -f

[root@test headTest]# ping localhost > ping.log&
[1] 4971
[root@test headTest]# tail -f ping.log
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.011 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.051 ms

通过CTRL+C结束实时监控

2.4 定时实时跟踪文件 -fs

[root@test headTest]# ping localhost > ping.log&
[2] 5116
[root@test headTest]# tail -fs2 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=10 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=220 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=221 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=222 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=223 ttl=64 time=0.081 ms

每隔2秒刷新,显示文件最新内容

2.5 从第5行开始显示剩余所有内容 -n +N

[root@test headTest]# tail -n +5 ping.log
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.081 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.050 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.029 ms
...省略...

2.6 显示多个文件

root@test headTest]# tail -n 5 hisotry.log ping.log 
==> hisotry.log <==
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log

==> ping.log <==
64 bytes from localhost (127.0.0.1): icmp_seq=331 ttl=64 time=0.062 ms
64 bytes from localhost (127.0.0.1): icmp_seq=332 ttl=64 time=0.030 ms
64 bytes from localhost (127.0.0.1): icmp_seq=333 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=334 ttl=64 time=0.024 ms
64 bytes from localhost (127.0.0.1): icmp_seq=335 ttl=64 time=0.025 ms

2.7 不显示文件名 -q

[root@test headTest]# tail -qn 5 hisotry.log ping.log 
  218  cd headTest/
  219  ls
  220  head --help
  221  history | head
  222  history > hisotry.log
64 bytes from localhost (127.0.0.1): icmp_seq=373 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=374 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=375 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=376 ttl=64 time=0.032 ms
64 bytes from localhost (127.0.0.1): icmp_seq=377 ttl=64 time=0.030 ms

你可能感兴趣的:(每天一个linux命令14-tail)