跟我一起写Shell脚本之十九--常用命令(tail)

1、介绍    

    tail也是我们经常在脚本中用到的一个命令,它和head类似,但是如名字所示,它主要用来显示文件的结尾部分内容。我们可以用man tail查看下它的介绍:

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 output starting with line NUM

       --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

       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.

       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.

2、用法总结

    我们用一个表格来总结下其用法:

编号 功能 命令
1 显示文件最后10行(默认10行) tail xxx1.txt
2 显示文件最后3行 tail -n 3 xxx1.txt
3 显示文件除了前三行的所有行 tail -n +3 xxx1.txt
4 显示文件最后3个字节 tail -c 3 xxx1.txt
5 显示文件除了前3个字节的所有字节 tail -c +3 xxx1.txt
6 不显示文件名(默认就不显示) tail -q xxx1.txt
7 显示文件名 tail -v xxx1.txt
8 显示多个文件的后5行内容 tail -n 5 xxx1.txt xxx2.txt
9 实时监控文件最后5行内容 tail -f -n 5 xxx1.txt
10 每隔3秒更新文件最后5行的内容 tail -f -s 3 -n 5 xxx1.txt
11 某个pid为$(PID)的进程结束后,停止刷新文件内容 tail -f --pid $(PID) -n 3 xxx1.txt

3、例子

    我们继续使用上一节介绍head时候的两个文件xxx1.txt和xxx2.txt做实验。

3.1 显示文件最后10行(默认10行)

$ tail xxx1.txt 
66
77
88
99
aa
bb
cc
dd
ee
ff

3.2 显示文件最后3行

$ tail -n 3 xxx1.txt 
dd
ee
ff

3.3 显示文件除了前三行的所有行

$ tail -n +3 xxx1.txt 
33
44
55
66
77
88
99
aa
bb
cc
dd
ee
ff

3.4 显示文件最后3个字节

$ tail -c 3 xxx1.txt 
ff

3.5 显示文件除了前3个字节的所有字节

$ tail -c +3 xxx1.txt 

22
33
44
55
66
77
88
99
aa
bb
cc
dd
ee
ff

3.6 显示文件名

$ tail -v xxx1.txt 
==> xxx1.txt <==
66
77
88
99
aa
bb
cc
dd
ee
ff

3.7 显示多个文件的后5行内容

$ tail -n 5 xxx1.txt xxx2.txt 
==> xxx1.txt <==
bb
cc
dd
ee
ff

==> xxx2.txt <==
bb
cc
dd
ee
ff
 

同样如果不想显示文件名要特地加-q:

$ tail -q -n 5 xxx1.txt xxx2.txt 
bb
cc
dd
ee
ff
bb
cc
dd
ee
ff
 

3.8 实时监控文件最后5行内容

一个终端输入tail -f -n 5 xxx2.txt, 另一个终端输入echo gg >> xxx2.txt, 结果可以看到更新:

$ tail -f -n 5 xxx2.txt
bb
cc
dd
ee
ff
gg
hh
 

$ echo gg >> xxx2.txt 
$ echo hh >> xxx2.txt 

好了,今天的部分就到这里了,接下来的Shell脚本之旅会更久精彩!

===================================================================================

注意:本文为本人原创,版权所属为个人所有,欢迎转载,但是转载请注明出处。

===================================================================================

Guess you like

Origin blog.csdn.net/sjwangjinbao/article/details/118229660