linux命令大全:head

2023年 7月 11日 24.4k 0

head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示文件的开头至标准输出中,而 tail 想当然尔就是看文件的结尾。

1.命令格式

head [参数]... [文件]...

Shell

2.命令功能

head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。

3.命令参数

  • -q 隐藏文件名
  • -v 显示文件名
  • -c 显示字节数
  • -n 显示的行数

4.使用实例

实例1:显示文件的前n行

命令:

head -n 5 log.log

Shell

演示操作及输出:

[yiibai@localhost test]$ cat log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
this is line 22.

-----------------end
[yiibai@localhost test]$ head -n 5 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

[yiibai@localhost test]$

Shell

实例2:显示文件前n个字节

命令:

head -c 20 log.log

Shell

演示操作及输出:

[yiibai@localhost test]$ head -c 20 log.log
this is line 1.
this[yiibai@localhost test]$

Shell

实例3:文件的除了最后n个字节以外的内容

命令:

head -c -32 log.log

Shell

演示操作及输出:

[yiibai@localhost test]$ head -c -32 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
[yiibai@localhost test]$

Shell

实例4:输出文件除了最后n行的全部内容

命令:

head -n -6 log.log
```shell
演示操作及输出:
```shell
[yiibai@localhost test]$ head -n -6 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
[yiibai@localhost test]$

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论