#######head、tail命令###########
head命令
用途:查看文件开头的一部分内容(默认为10行)
格式:head -n number 文件名
格式:head -数字 文件名
tail命令
用途:查看文件结尾的少部分内容(默认为10行)
格式:tail -n number 文件名
格式:tail -数字 文件名
tail -f 文件名 = tailf
[root@hp lianxi]# cat test
1a
2b
3c
4d
5e
6f
7g
8h
9i
10j
11k
12l
13m
[root@hp lianxi]# head -n 2 test
1a
2b
[root@hp lianxi]# head -2 test
1a
2b
[root@hp lianxi]#
[root@localhost lianxi]# tail -n +10 test
10j
11k
12l
13m
[root@localhost lianxi]# tail -n 2 test
12l
13m
#查看文件的5到7行
[root@localhost lianxi]# head -n 7 test | tail -n 3
#管道符号:
上一条命令的输出,成为下一条命令的输入
格式:tail -n 数字 文件名
数字:数字前有 +(加号),从文件开头指定的单元 数开始输出;数字前有 -(减号),从文件末尾指定的单元 数开始输出;没有 +或 -,从文件末尾指定的单元 数开始输出。
例如:
tail -n +3 /etc/passwd 从第三行开始显示
tail -n -3 /etc/passwd 显示最后三行
head -n 3 /etc/passwd 显示前三行
head -n -3 /etc/passwd 显示倒数第三行之前
#tail -f 实时监控文件追加信息 (按ctrl+c终止)
[root@localhost log]# pwd
/var/log
[root@localhost log]# tail -f messages