Linux tail命令详解

tail命令用于从文件的尾部读取指定数量的数据行,并将其显示在屏幕上,默认查看10行数据。

命令格式:
tail [OPTION]… [FILE]…

选项:
-n, –lines=K:显示文件末尾的K行;
-f, –follow:跟踪文件的末尾,实时显示新增的内容;
-q, –quiet:不显示头部信息;
-v, –verbose:显示头部信息;
-c, –bytes=K:显示文件末尾的K字节;
-z, –zero-terminated:以NUL字符作为行结束符;

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.    

例子:

例1,查看文件末尾内容
study@DESKTOP:/var/log$ tail dpkg.log
2022-03-24 22:18:46 status installed x11-utils:amd64 7.7+5
2022-03-24 22:18:46 trigproc systemd:amd64 245.4-4ubuntu3.15 <none>
2022-03-24 22:18:46 status half-configured systemd:amd64 245.4-4ubuntu3.15
2022-03-24 22:18:46 status installed systemd:amd64 245.4-4ubuntu3.15
2022-03-24 22:18:46 trigproc man-db:amd64 2.9.1-1 <none>
2022-03-24 22:18:46 status half-configured man-db:amd64 2.9.1-1
2022-03-24 22:18:46 status installed man-db:amd64 2.9.1-1
2022-03-24 22:18:46 trigproc libc-bin:amd64 2.31-0ubuntu9.7 <none>
2022-03-24 22:18:46 status half-configured libc-bin:amd64 2.31-0ubuntu9.7
2022-03-24 22:18:46 status installed libc-bin:amd64 2.31-0ubuntu9.7    

例2,查看文件末尾,按照指定行数查看
study@DESKTOP:/var/log$ tail -5 dpkg.log    
2022-03-24 22:18:46 status half-configured man-db:amd64 2.9.1-1
2022-03-24 22:18:46 status installed man-db:amd64 2.9.1-1
2022-03-24 22:18:46 trigproc libc-bin:amd64 2.31-0ubuntu9.7 <none>
2022-03-24 22:18:46 status half-configured libc-bin:amd64 2.31-0ubuntu9.7
2022-03-24 22:18:46 status installed libc-bin:amd64 2.31-0ubuntu9.7