tail 命令可用于查看文件的内容。

最常用到的是参数 -f ,用于查阅正在改变的日志文件。

命令格式:

tail [参数] [文件] 

参数:

  • -f 循环读取,监视文件的尾部内容(默认10行,相当于增加参数 -n 10)
  • -q 不显示处理信息
  • -v 显示详细的处理信息
  • -c<数目> 显示的字节数
  • -n<行数> 显示文件的尾部 n 行内容
  • –pid=PID 与-f合用,表示在进程ID,PID死掉之后结束
  • -q, --quiet, --silent 从不输出给出文件名的首部
  • -s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

可以执行命令tail --help就能看到tail命令介绍

-c, --bytes=K            output the last K bytes;
                           or use -c +K to output bytes starting with the Kth of each file
                           输出最后的 K 个字节;
			               或者使用 -c +K 从每个文件的第K字节开始打印。
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           an absent option argument means 'descriptor'
                           随着文件的增长,输出附加数据;(动态输出最新的信息);
                           没有选项参数意味着“描述符”
                           
  -F                       same as --follow=name --retry
                           与 --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
                           输出最后的K行,而不是最后的10行;
                           或者使用-n +K从第K个开始输出
                           
      --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
                           使用——follow=name,在N次(默认为5次)迭代后,重新打开一个大小没有改变的文件,看看它是否被解除链接或重命名(这是旋转日志文件的常见情况);
                           对于inotify,这个选项很少有用
                             
      --pid=PID            with -f, terminate after process ID, PID dies
                           与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令
                           
  -q, --quiet, --silent    never output headers giving file names
                           当有多个文件参数时,不输出各个文件名
                           
      --retry              keep trying to open a file if it is inaccessible
                           即是在tail命令启动时,文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件。使用此选项时需要与选项“——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
                           与“-f”选项连用,指定监视文件变化时间隔的秒数(默认为1.0);
			               使用inotify和-pid=P,每N秒检查进程P至少一次
			               
  -v, --verbose            always output headers giving file names
                           当有多个文件参数时,总是输出各个文件名
                           
      --help               display this help and exit
                           显示此帮助信息并退出
                           
      --version            output version information and exit
                           显示版本信息并退出

其中 tail -f与tail -F的区别:
tail -f

等同于–follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止

tail -F

等同于–follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪

tailf

等同于tail -f -n 10(貌似tail -f或-F默认也是打印最后10行,然后追踪文件),与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件,减少了磁盘访问,所以tailf特别适合那些便携机上跟踪日志文件。

如果遇到日志文件较大的时候,通常会输出的行数来查看日志:

// 默认显示 log 文件的最后 10tail test.log
 
// 显示 log 文件的最后 10 行,同时跟踪名为文件的增长情况,直到您按下(Ctrl-C)组合键停止显示。
tail -f test.log
 
// 显示 log 文件的最后 n 行,同时跟踪名为文件的增长情况,直到您按下(Ctrl-C)组合键停止显示。
tail -nf test.log
 
// 显示文件的最后 10tail -n 10 filename
 
// 文件的第 9 行不显示,显示第 10 行到末尾行
tail -n -10 filename
 
// 显示文件的第 10 行到末尾行
tail -n +10 filename
 
逆序显示filename最后10行。
tail -r -n 10 filename
 
// 显示第20行至末尾
tail +20 test.log
 
// 显示最后10个字符
tail -c 10 test.log

退出:
按下CTRL+C。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐