基础命令学习目录首页

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

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

一、head命令详解

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

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 lastK bytes of each file-n, --lines=[-]K         print the first K lines instead of the first 10;with the leading `-', print all but the lastK 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 exitK 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.

head命令的语法格式为:

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

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

实例:

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

新建test.txt,共14行.

amosli@amosli-pc:~/learn/fd$ cat -n test.txt 1    a2    b3    c4    d5    e6    f7    g8    h9    i10    j11    k12    l13    m14    n

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

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

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行外的所有内容

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

英文提示信息:

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

加上'-',打印所有内容除了最后的K行。

4.-c参数,按文件内容大小来打印,打印前2个字节的内容

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

2个字节就是一个“a”字母。

英文提示信息:

  -c, --bytes=[-]K         print the first K bytes of each file;

5.-c参数,打印除了最后2个字节的文件内容

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

英文提示信息:

  -c, --bytes=[-]K         print the first K bytes of each file;with the leading `-', print all but the lastK bytes of each file

6.-q参数,打印时不显示文件名称

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

英文提示信息:

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

其实后面跟上--quiet,--silent都是一样的,都不会显示文件名称,和默认打印是一样的效果。

7.-v参数,打印是显示文件名称

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

英文提示信息:

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

其中,用--verbose和-v显示的是一样的效果

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

8.打印多个文件的内容

amosli@amosli-pc:~/learn/fd$ head -n 3 test.txt test2.txt
==> test.txt <==
a
b
c==> test2.txt <==
c
d
e

二、tail命令详解

tail命令和head 命令非常相似,只不过它是打印文件的尾部内容的,当然也有一些特色之处,下面一起来看看吧。

首先,输入tail --help看一下提示信息

amosli@amosli-pc:~/learn/fd$ tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 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            output the last K bytes; alternatively, use -c +Kto output bytes starting with the Kth of each file-f, --follow[={name|descriptor}]output appended data as the file grows;-f, --follow, and --follow=descriptor areequivalent-F                       same as --follow=name --retry-n, --lines=K            output the last K lines, instead of the last 10;or use -n +K to output lines starting with the Kth--max-unchanged-stats=Nwith --follow=name, reopen a FILE which has notchanged size after N (default 5) iterationsto see if it has been unlinked or renamed(this is the usual case of rotated log files).With inotify, this option is rarely useful.--pid=PID            with -f, terminate after process ID, PID dies-q, --quiet, --silent    never output headers giving file names--retry              keep trying to open a file even when it is orbecomes inaccessible; useful when following byname, i.e., with --follow=name-s, --sleep-interval=N   with -f, sleep for approximately N seconds(default 1.0) between iterations.With inotify and --pid=P, check process P atleast once every N seconds.-v, --verbose            always output headers giving file names--help     display this help and exit--version  output version information and exitIf the first character of K (the number of bytes or lines) is a `+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  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.With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

语法格式:

tail [OPTION]... [FILE]...

这里由于head和tail实在比较像,这里为了节省篇幅,对相似之处将尽量简洁

实例:

test2.txt,共有12行内容为从c-n

amosli@amosli-pc:~/learn/fd$ cat -n test2.txt 1    c2    d3    e4    f5    g6    h7    i8    j9    k10    l11    m12    n

1.-c 参数,根据文件字节进行输出打印

  -c, --bytes=K            output the last K bytes; alternatively, use -c +Kto output bytes starting with the Kth of each file

打印test2.txt中的最后4 bytes,如下:

amosli@amosli-pc:~/learn/fd$ tail -c 4 test2.txt
m
n

tail -c +4 test2.txt 加上一个‘+’会是什么效果呢?

amosli@amosli-pc:~/learn/fd$ tail -c +4 test2.txt e
f
g
h
i
j
k
l
m
n

少打印了c d 两个字母,那么 -c +K的意思也就很明了了,即打印文件的所有内容除了前面的K个字节

2、-n参数,根据文件行数进行打印

看一下提示信息:

 -n, --lines=K            output the last K lines, instead of the last 10;or use -n +K to output lines starting with the Kth--max-unchanged-stats=Nwith --follow=name, reopen a FILE which has notchanged size after N (default 5) iterationsto see if it has been unlinked or renamed(this is the usual case of rotated log files).With inotify, this option is rarely useful.--pid=PID            with -f, terminate after process ID, PID dies

打印test2.txt最后的3行内容:

amosli@amosli-pc:~/learn/fd$ tail -n 3 test2.txt
l
m
n

从第3行开始输出test2.txt的所有内容:

amosli@amosli-pc:~/learn/fd$ tail -n +3 test2.txt
e
f
g
h
i
j
k
l
m
n

3.-q参数,-v参数

 -q, --quiet, --silent    never output headers giving file names--retry              keep trying to open a file even when it is orbecomes inaccessible; useful when following byname, i.e., with --follow=name

不打印文件名称信息:

amosli@amosli-pc:~/learn/fd$ tail -q  test2.txt
e
f
g
h
i
j
k
l
m
n

打印文件名称信息:

amosli@amosli-pc:~/learn/fd$ tail -v test2.txt
==> test2.txt <==
e
f
g
h
i
j
k
l
m
n

4、-f参数

tail 命令的一个很重要的用法是从一个内容不断增加的文件中读取数据。新增加的内容部民被添加到文件的尾部,因此当新内容被写入文件的时候,可以用tail将其显示出来。只是简单的使用tail的话,它会读取文件的最后10行,然后退出,这样就不能做到实时监控,加入-f参数就可以做到实时监控文件的更新内容了。

amosli@amosli-pc:~/learn/fd$ tail -f test2.txt
g
h
i
j
k
l
m
n
o
p

ctrl+alt+t新开一个终端,然后执行下面的命令:

amosli@amosli-pc:~/learn/fd$ echo  'xyz' >> test2.txt

这个时候你就可以看到前一个终端在里出现了‘xyz’

amosli@amosli-pc:~/learn/fd$ tail -f test2.txt
g
h
i
j
k
l
m
n
o
p
xyz

这样就能实时监控项目里的log日志了。

如果想设定一个间隔时间去查看文件的更新应该怎么做呢?请看-s参数

5、-s参数

英文提示信息:

-s, --sleep-interval=N   with -f, sleep for approximately N seconds(default 1.0) between iterations.With inotify and --pid=P, check process P atleast once every N seconds.

如每隔5秒查看一次test2.txt的内容是否更新

amosli@amosli-pc:~/learn/fd$ tail -f -s 5 test2.txt
j
k
l
m
n
o
p
xyz

默认是1秒更新一次。

6.--pid参数

tail 具有一个很意思的特性,当某个给定进程结束之后,tail也会随之终结.

假如我们正在读取一个不断增长的文件,进程Foo一直在向该文件追加数据,那么tail就会一直执行,直到进程Foo的结束.

$PID=$(pidof Foo)
$tail -f file --pid $PID
#当进程Foo结束之后,tail也会跟着结束

例如用gedit打开test2.txt,不断加入数据,然后在终端里使用tail进行监听,具体为:

amosli@amosli-pc:~/learn/fd$ PID=$(pidof gedit)
amosli@amosli-pc:~/learn/fd$ tail -f -s 2 test2.txt --pid $PID
h
i
j
k
l
m
n
o
p
xyz

然后不断在gedit中追加入‘yyy’后保存,按理说终端里应该会更新,但我的终端不知为何没有更新数据,这里就不帖出来了。

关闭gedit后,tail监控也关闭掉了。

head和tail取文件第m行到第n行

[root@xiaoma /root/mcw] test!
#vim test.txt
[root@xiaoma /root/mcw] test!
#cat test.txt
1 ma
2 chang
3 wei
4 mo
5 jiang
[root@xiaoma /root/mcw] test!
#cat test.txt |head -4|tail -2 >test1.txt
[root@xiaoma /root/mcw] test!
#cat test1.txt
3 wei
4 mo

转载于:https://www.cnblogs.com/machangwei-8/p/9570818.html

head和tail命令详解相关推荐

  1. linux的退出tail命令,Linux显示文件最后几行-Linux tail命令详解-Linux tail命令退出-嗨客网...

    Linux显示文件最后几行(tail) Linux显示文件最后几行(tail)教程 Linux tail命令详解 功能 查看文件的最后几行. 语法 tail [options] file.. 参数 参 ...

  2. Linux tail 命令详解

    用途 从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到 ...

  3. [一文一命令]tail命令详解

    tail 名称:tail - output the last part of files 输出文件最后的那部分内容 功能描述:tail命令从指定点开始将文件写到标准输出,使用-f选项可以方便的查阅正在 ...

  4. linux tail命令详解

    linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档 ...

  5. Linux工作笔记034---linux tail命令详解_linux在vi中查找字符串

    JAVA技术交流QQ群:170933152 使用vi编辑器编辑长文件时,常常是头昏眼花,也找不到需要更改的内容. 这时,使用查找功能尤为重要. 方法如下: 1.命令模式下输入"/字符串&qu ...

  6. linux tail 命令详解,Linux下如何使用tail命令指南

    如果head命令用于查看文件的头部,那么tail命令就正好相反,tail命令主要用于查看文件内容的结尾,下面小编就给大家详细介绍下Linux下tail的用法吧. 1.命令格式; tail[必要参数][ ...

  7. linux命令tail-f,【玩转Linux命令】tail命令详解

    Hi,明娜桑,哦哈哟~. 今天小编给大家带来的"玩转linux命令之tail篇",tail命令是咱们工作中经常用到的命令,常用于动态文件的查看,接下来搜狗华安带你来一起了解tail ...

  8. Linux命令-1:tail命令详解

    由于测试过程中经常需要实时查看Tomcat日志,所以要掌握tail命令的用法.将平时用到的在此记录汇总一下. 实时打印文件内容 tail -f filename 显示文件的最后10行 tail fil ...

  9. Linux中head和tail的区别,linux head和tail命令详解

    1.head命令 回到顶部 head命令显示文件的开头几行,常用命令格式如下: [root@initroot ~]# head [-n number] 文件 head命令默认显示文件的前十行,-n选项 ...

最新文章

  1. VS编译时output/Error list窗口自动弹出设置
  2. 计算机网络按信息传输介质的性能来划分,大学计算机基础练习题网络技术.doc...
  3. leetcode338 比特位计数
  4. 生日在java中怎么写_如何用java写代码:输入自己的生日,距离今天已过多少天,或者还有...
  5. php 保護連接字符串,PHP字符串操作
  6. 计算机表格中需要乘法求和,《怎么在Excel表格里面使用乘法求和》
  7. maven settings.xml 包含多个镜像库
  8. 计算机 科研进度安排,研究计划进度安排及预期目标-浙江大学现代教务管理系统.doc...
  9. 发送文件夹不存在或被删除
  10. xp系统本地连接服务器,xp系统本地连接受限制或无连接怎么办丨xp本地连接断开无法上网解决办法...
  11. 【天池竞赛系列】菜鸟-需求预测与分仓规划初赛冠军解决方案
  12. md文件打开错误(Failed to load file)
  13. 志愿者管理系统 php,志愿者信息管理系统
  14. 利用OpenCV识别图片背景是否透明
  15. 如何判断微信/判断支付宝退款超期
  16. 用PS调出人像冷色艺术效果
  17. 报错:Loading mirror speeds from cached hostfile解决方案
  18. Airbin_0720
  19. 保姆级教程带你在VMware中安装Windows11操作系统
  20. unity中字体Canvas的字体模糊与清晰

热门文章

  1. FFmpeg中libswscale库简介及测试代码
  2. linux如何安装neo4j,Ubuntu16.04 如何安装neo4j数据库
  3. python接收输入的一行字符只统计数字的个数,Python(统计字符),python实例,输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数...
  4. kali扫描内网ip_来,我们聊聊内网渗透!
  5. linux cp 强制覆盖_Linux基本操作教程
  6. 【web】将一个jar包更改成war包
  7. Tomcat下载安装与环境变量的配置
  8. iOS SwiftUI篇-4 注解@State、@Binding、@ObservedObject、@EnvironmentObject、@Environment
  9. cordova 环境配制和创建插件
  10. 快速构建Spring Cloud工程