简介

head命令将一个或多个文件或管道数据(默认为10行)打印到标准输出。

命令格式

head [OPTION]… [FILE]…

常用参数

-n [-]K 显示前K行。如果K前有-,则表示显示除最后K行外的所有行
-c [-]K 显示文件前K字节。如果K前有-,则表示显示除最后K字节外的所有内容
可以在数字后面使用乘数后缀来指定要显示的字节数。
**b**乘以512,**kB**乘以1000,**K**乘以1024,**MB**乘以1000000,**M**乘以1048576
-v 显示文件名称
-q 不显示文件名

示例

  1. [root@localhost ~]# cut -d ':' -f 1 /etc/passwd | head -n 11 > test.txt
  2. [root@localhost ~]# cat test.txt
  3. root
  4. bin
  5. daemon
  6. adm
  7. lp
  8. sync
  9. shutdown
  10. halt
  11. mail
  12. operator
  13. games
  14. [root@localhost ~]# wc test.txt
  15. 11 11 62 test.txt
  16. ## -n 显示多少行 默认显示 10 行
  17. [root@localhost ~]# head -n 10 test.txt
  18. root
  19. bin
  20. daemon
  21. adm
  22. lp
  23. sync
  24. shutdown
  25. halt
  26. mail
  27. operator
  28. [root@localhost ~]# head -n -10 test.txt
  29. root
  30. ## -c 显示多少字节
  31. [root@localhost ~]# head -c 4 test.txt
  32. root[root@localhost ~]#
  33. [root@localhost ~]# head -c -58 test.txt ## -58字节
  34. root[root@localhost ~]#
  35. ## -v 显示文件名称
  36. [root@localhost ~]# head -n 1 -v test.txt
  37. ==> test.txt <==
  38. root
  39. ## -q 不显示文件名
  40. [root@localhost ~]# head -n 1 -q test.txt
  41. root