APP下载

Linux使用tail命令检视档案内容最后N行实用案例

消息来源:baojiabao.com 作者: 发布时间:2024-05-14

报价宝综合消息Linux使用tail命令检视档案内容最后N行实用案例

1. tail命令简介

本文介绍tail命令,该命令与head命令刚好相反,head命令是检视档案的开头N行内容,而tail命令是检视档案最后N行的内容。当然它有一个非常有用的选项-f,常用于跟踪日志档案的实时变化。

2. tail命令的选项

-f 循环读取

-q 不显示处理资讯

-v 显示详细的处理资讯

-c 显示的字节数

-n 显示行数

--pid=PID 与-f合用,表示在程序ID,PID死掉之后结束.

-q, --quiet, --silent 从不输出给出档名的首部

-s, --sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒

3. 常用案例

3.1 tail命令预设输出

[[email protected] ~]# tail /etc/nginx/nginx.conf

types_hash_max_size 2048;

include /etc/nginx/mime.types;

default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

Linux tail命令检视文字内容预设输出

可以看出,预设情况下,tail命令预设输出档案的最后10行。

3.2 指定输出行数

检视档案nginx.conf的最后5行

[[email protected] ~]# tail -n 5 /etc/nginx/nginx.conf

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

Linux tail命令指定行数检视文字内容

3.3 跟踪日志的变化

对于程式执行的排错诊断非常有用!

实时跟踪nginx的日志变化。

[[email protected] ~]# tail -f /var/log/nginx/error.log

2018/11/28 15:01:27 [error] 31908#0: *3829 open "/usr/share/nginx/html/rehat/ws/v1/cluster/apps/new-application" failed (2: No such file or directory), client: 1.1.1.1, server: rehat.exmaple.com, request: "POST /ws/v1/cluster/apps/new-application HTTP/1.1", host: "127.0.0.1:80"

2018/11/28 15:01:32 [error] 31908#0: *3810 open "/usr/share/redhat/html/example/manager/html" failed (2: No such file or directory), client: 1.1.1.1, server:redhat.example.com, request: "GET /manager/html HTTP/1.1", host: "127.0.0.1"

2018/11/28 15:35:15 [error] 31908#0: *3838 open "/usr/share/nginx/html/cisco/robots.txt" failed (2: No such file or direct

可以出中诊断出程式的问题,当然以上仅仅是演示,根据自己遇到的情况进行判断。

跟踪系统日志:

[[email protected] ~]# tail -f /var/log/messages

Linux tail命令跟踪文字内容变化

4. 总结

tail命令是Linux系统检视文字内容的重要工具之一,特别是选项-f,可以实时跟踪日志文字的变化。

2020-01-20 02:48:00

相关文章