head和tail命令详解

基础命令学习目录首页

原文链接:https://www.cnblogs.com/amosli/p/3496027.html

当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除了前n行或后n行之外的所有行,也有可能需要实时监控log日志的更新,那么怎么实现呢?下面一起来看一下linux下使用率极高的head ,tail两个命令。

一、head命令详解

首先,输入head --help查看帮助信息:

[复制代码](javascript:void(0)😉

复制代码

amosli@amosli-pc:~/learn/fd$ head --help
Usage: head [OPTION]... [FILE]...
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    never print headers giving file names
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

K 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.

复制代码

[复制代码](javascript:void(0)😉

回到顶部

head命令的语法格式为:

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

接下来将在实例中讲解各参数含义及用法:

回到顶部

实例:

1.使用head命令查看文件内容前十行

新建test.txt,共14行.

[复制代码](javascript:void(0)😉

复制代码

amosli@amosli-pc:~/learn/fd$ cat -n test.txt 
     1    a
     2    b
     3    c
     4    d
     5    e
     6    f
     7    g
     8    h
     9    i
    10    j
    11    k
    12    l
    13    m
    14    n

复制代码

[复制代码](javascript:void(0)😉

使用head命令查看前十行,head命令默认显示文件前十行

[复制代码](javascript:void(0)😉

复制代码

amosli@amosli-pc:~/learn/fd$ head test.txt 
a
b
c
d
e
f
g
h
i
j

复制代码

[复制代码](javascript:void(0)😉

2.-n参数,显示test.txt文件的前3行

amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt 
a
b
c

英文提示信息:

  -n, --lines=[-]K         print the first K lines instead of the first 10;                   

3.-n参数显示除了文件最后3行外的所有内容

[复制代码](javascript:void(0)😉

复制代码

amosli@amosli-pc:~/learn/fd$ head -n -3 test.txt 
a
b
c
d
e
f
g
h
i
j
k

复制代码

[

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值