0022. shell命令--head

目录

22. shell命令--head

功能说明

语法格式

选项说明

实践操作

注意事项


22. shell命令--head

功能说明

        head 是一个在 Unix 和 Linux 系统中常用的命令行工具,用于显示文件的开头部分。默认情况下,head 命令会显示文件的前 10 行。但是,你可以使用 -n 选项来指定要显示的行数。

语法格式

head [参数] [文件]  

SYNOPSIS
       head [OPTION]... [FILE]...

选项说明

  • -c, --bytes=[-]NUM:显示前NUM字节;如果NUM前有"-",那么会打印除了文件末尾的NUM字节以外的其他内容。
  • -n, --lines=[-]NUM:显示前NUM行而不是默认的10行;如果NUM前有"-",那么会打印除了文件末尾的NUM行以外的其他行。
  • -q, --quiet, --silent:不打印文件名行。
  • -v, --verbose:总是打印文件名行。
  • -z, --zero-terminated:行终止符为NUL而不是换行符。
  • --help:显示帮助信息并退出。
  • --version:显示版本信息并退出。
  • NUM可以有一个乘数后缀:
    • b 512
    • kB 1000
    • k 1024
    • MB 10001000M 10241024
    • GB 100010001000
    • G 102410241024
    • T、P、E、Z、Y等以此类推。
  • 也可以使用二进制前缀:
    • KiB=K
    • MiB=M
    • 以此类推。

实践操作

1. 默认显示文件的前N行内容
mkdir -p /test/head
cd /test/head
seq 20 >file
head file
head -n 10 file

2. 显示指定文件的前N行内容
head -n5 file
head -15 file    #-n参数可以省略
head -n 5 ~/.bash_history ~/.bashrc    #查看多个文件

3. 显示指定文件的前N个字符
head -2 /etc/passwd
head -c 20 /etc/passwd

4. 其它相关操作
head -v file    #显示文件名
head --help
head --version

命令示例:1. 默认显示文件的前N行内容

mkdir -p /test/head
cd /test/head
seq 20 >file
head file
head -n 10 file

输出结果:

[root@MineGi ~]# mkdir -p /test/head
[root@MineGi ~]# cd /test/head
[root@MineGi /test/head]# seq 20 >file
[root@MineGi /test/head]# head file
1
2
3
4
5
6
7
8
9
10
[root@MineGi /test/head]# head -n 10 file
1
2
3
4
5
6
7
8
9
10
[root@MineGi /test/head]#

命令示例:2. 显示指定文件的前N行内容

head -n5 file
head -15 file
head -n 5 ~/.bash_history ~/.bashrc

输出结果:

[root@MineGi /test/head]# head -n5 file
1
2
3
4
5
[root@MineGi /test/head]# head -15 file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@MineGi /test/head]# head -n 5 ~/.bash_history ~/.bashrc
==> /root/.bash_history <==
curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
ls /etc/yum.repos.d/
yum install tree nmap dos2unix lrzsz nc lsof wget tcpdump htop iftop iotop sysstat nethogs net-tools telnet psmisc bash-completion vim-enhanced -y
yum install chrony -y

==> /root/.bashrc <==
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
[root@MineGi /test/head]#

命令示例:3. 显示指定文件的前N个字符

head -2 /etc/passwd
head -c 20 /etc/passwd

输出结果:

[root@MineGi /test/head]# head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@MineGi /test/head]# head -c 20 /etc/passwd
root:x:0:0:root:/roo[root@MineGi /test/head]#

命令示例:4. 其它相关操作

head -v file 
head --help
head --version

输出结果:

[root@MineGi /test/head]# head -v file 
==> file <==
1
2
3
4
5
6
7
8
9
10
[root@MineGi /test/head]# head --help
用法:head [选项]... [文件]...
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=[-]K         print the first K bytes of each file;
                             with the leading '-', print all but the last
                             K bytes of each file
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading '-', print all but the last
                             K lines of each file
  -q, --quiet, --silent    不显示包含给定文件名的文件头
  -v, --verbose        总是显示包含给定文件名的文件头
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

K 后面可以跟乘号:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, 对于T, P, E, Z, Y 同样适用。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告head 的翻译错误
要获取完整文档,请运行:info coreutils 'head invocation'
[root@MineGi /test/head]# head --version
head (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。

由David MacKenzie 和Jim Meyering 编写。
[root@MineGi /test/head]# 

注意事项

  • 如果指定的行数大于文件中的实际行数,head 命令会显示整个文件的内容。
  • head 命令通常与 tail 命令一起使用,tail 命令用于显示文件的末尾部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MineGi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值