head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾。
head命令
head 命令可用于查看文件的开头部分的内容,有一个常用的参数 -n 用于显示行数,默认为 10,即显示 10 行的内容。
命令格式:
head [参数] [文件]
参数:
- -q 隐藏文件名
- -v 显示文件名
- -c<数目> 显示的字节数。
- -n<行数> 显示的行数。
[wang@localhost ~]$ head textfile2
1 11111111111111111
2 22222222222222222
3 3
4
5
6 4444444444444444
7 5555555555555555
8 6666666666666666
9
10
-- 显示 textfile2 文件的开头 5 行
[wang@localhost ~]$ head -n 5 textfile2
1 11111111111111111
2 22222222222222222
3 3
4
5
-- 显示文件前 20 个字节:
[wang@localhost ~]$ head -c 20 textfile2
1 1111111111111[wang@localhost ~]$
-- 文件的除了最后n个字节以外的内容
[wang@localhost ~]$ cat textfile2
1 11111111111111111
2 22222222222222222
3 3
4
5
6 4444444444444444
7 5555555555555555
8 6666666666666666
9
10
11 99999999999999
eeeeeeeeeeeeeeeeeee
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeZZ9
-- 文件的除了最后20个字节以外的内容
[wang@localhost ~]$ head -c -20 textfile2
1 11111111111111111
2 22222222222222222
3 3
4
5
6 4444444444444444
7 5555555555555555
8 6666666666666666
9
10
11 99999999999999
eeeeeeeeeeeeeeeeeee
eeeeeeeeeeeee[wang@localhost ~]$
-- 输出文件除了最后n行的全部内容
[wang@localhost ~]$ head -n -5 textfile2
1 11111111111111111
2 22222222222222222
3 3
4
5
6 4444444444444444
7 5555555555555555
8 6666666666666666
[wang@localhost ~]$
tail命令
tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容.
命令格式:
tail [参数] [文件]
参数:
- -f 循环读取
- -q 不显示处理信息
- -v 显示详细的处理信息
- -c<数目> 显示的字节数
- -n<行数> 显示文件的尾部 n 行内容
- –pid=PID 与-f合用,表示在进程ID,PID死掉之后结束
- -q, –quiet, –silent 从不输出给出文件名的首部
- -s, –sleep-interval=S 与-f合用,表示在每次反复的间隔休眠S秒
显示文件末尾内容
-- 显示文件最后5行内容
[push_service_gateway-00 push_service]$ tail -n 5 push_service_gateway.log
[] 2021-08-22 02:18:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:18:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[rd@线上-基础架构-车源-话务-push_service_gateway-00 push_service]$
-- 循环查看文件内容,一般用于查看日志
[push_service_gateway-00 push_service]$ tail -f push_service_gateway.log
[] 2021-08-22 02:17:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:18:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:18:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:18:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:19:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
-- 从第5行开始显示文件
[push_service_gateway-00 push_service]$ tail -f -n5 push_service_gateway.log
[] 2021-08-22 02:21:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:21:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:22:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:22:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新
[] 2021-08-22 02:22:30 - [INFO] [LocalMonitor:71 watchFile] 非受监控的文件发生变更,无需刷新