Linux head命令详解

head命令用于从文件的开头处显示指定数量的行。该命令默认显示文件的前10行。

命令格式:
head [OPTION]… [FILE]…

选项:
-n, –lines=K 显示K行
-c, –bytes=K 显示K字节
-q, –quiet 不显示文件名
-v, –verbose 显示文件名
-z, –zero-terminated 以NUL字符结尾

DESCRIPTION
       Print the first 10 lines of each FILE to standard output.  With more than one FILE, precede each with a header
       giving the file name.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -c, --bytes=[-]NUM
              print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of  each
              file

       -n, --lines=[-]NUM
              print  the  first  NUM  lines instead of the first 10; with the leading '-', print all but the last NUM
              lines of each file

       -q, --quiet, --silent
              never print headers giving file names

       -v, --verbose
              always print headers giving file names

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000,  M  1024*1024,  GB  1000*1000*1000,  G
       1024*1024*1024, and so on for T, P, E, Z, Y.
例1,查看文件开头,按照默认行数查看
study@DESKTOP:/var/log$ head dpkg.log
2022-03-24 21:47:25 startup packages remove
2022-03-24 21:47:25 status installed linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 remove linux-virtual:amd64 5.4.0.105.109 <none>
2022-03-24 21:47:25 status half-configured linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 status half-installed linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 status config-files linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 status not-installed linux-virtual:amd64 <none>
2022-03-24 21:47:25 status installed linux-headers-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 remove linux-headers-virtual:amd64 5.4.0.105.109 <none>
2022-03-24 21:47:25 status half-configured linux-headers-virtual:amd64 5.4.0.105.109

例2,查看文件开头,按照指定行数查看
study@DESKTOP:/var/log$ head -5 dpkg.log
2022-03-24 21:47:25 startup packages remove
2022-03-24 21:47:25 status installed linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 remove linux-virtual:amd64 5.4.0.105.109 <none>
2022-03-24 21:47:25 status half-configured linux-virtual:amd64 5.4.0.105.109
2022-03-24 21:47:25 status half-installed linux-virtual:amd64 5.4.0.105.109