head 命令的功能是显示文件开头的内容,默认为前10行。
一、语法格式
head [参数] 文件
二、常用参数
-n <数字> | 定义显示行数 |
-c <数字> | 指定显示头部内容的字符数 |
-v | 总是显示文件名的头信息 |
-q | 不显示文件名的头信息 |
三、命令实战
3.1 默认显示文件前10行
[root@linux1 ~]# head /var/log/messages
Apr 23 11:01:01 linux1 systemd: Started Session 3 of user root.
Apr 23 12:01:01 linux1 systemd: Started Session 4 of user root.
Apr 23 13:11:10 linux1 kernel: e1000: ens32 NIC Link is Down
Apr 23 13:11:10 linux1 systemd: Time has been changed
Apr 23 13:11:14 linux1 chronyd[803]: Forward time jump detected!
Apr 23 13:11:14 linux1 chronyd[803]: Can't synchronise: no selectable sources
Apr 23 13:11:16 linux1 kernel: e1000: ens32 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
Apr 23 13:11:16 linux1 NetworkManager[783]: <info> [1682226676.2885] device (ens32): carrier: link connected
Apr 23 13:14:29 linux1 chronyd[803]: Selected source 119.28.183.184
Apr 23 13:16:38 linux1 chronyd[803]: Selected source 202.118.1.81
3.2 指定显示文件前5行内容
[root@linux1 ~]# head -n 5 /var/log/messages
Apr 23 11:01:01 linux1 systemd: Started Session 3 of user root.
Apr 23 12:01:01 linux1 systemd: Started Session 4 of user root.
Apr 23 13:11:10 linux1 kernel: e1000: ens32 NIC Link is Down
Apr 23 13:11:10 linux1 systemd: Time has been changed
Apr 23 13:11:14 linux1 chronyd[803]: Forward time jump detected!
3.3 指定显示文件前200个字符
[root@linux1 ~]# head -c 200 /var/log/messages
Apr 23 11:01:01 linux1 systemd: Started Session 3 of user root.
Apr 23 12:01:01 linux1 systemd: Started Session 4 of user root.
Apr 23 13:11:10 linux1 kernel: e1000: ens32 NIC Link is Down