当前位置: 首页 > 编程日记 > 正文

head和tail命令详解

基础命令学习目录首页

原文链接: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

相关文章:

【FFmpeg】ffmpeg工具源码分析(四):filter(过滤器、滤镜)详解

1、简介 FFmpeg用来处理音视频,实现处理功能的核心就是filter(滤镜),和我们使用的美颜功能的滤镜意思差不多,FFmpeg的filter(滤镜)不仅可以处理视频,还能处理音频、字幕等。 官方说明: 在编码之前,ffmpeg可以使用 libavfilter 库中的过滤器处理原始音频和视频帧。几…

【ZooKeeper Notes 14】数据模型

转载请注明&#xff1a;ni掌柜 nileadergmail.com本文主要讲述ZooKeeper的数据模型&#xff0c;包括ZooKeeper的数据视图&#xff0c;节点的层次结构以及节点类型等基本属性。Zookeeper的视图结构类似标准的Unix文件系统&#xff0c;但是没有引入文件系统相关概念&#xff1a;目…

李理:为什么说人工智能可以实现?

作者 | Just出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;尽管市面上关于深度学习的书籍很多&#xff0c;环信 AI 负责人李理认为大部分只关注理论或只关注实践。于是&#xff0c;基于他对深度学习多年的理解&#xff0c;自己着整理手写了一本深度学习理论与实战书…

【FFmpeg】FFmpeg编解码H264产生马赛克、伪影的解决方法

1、问题描述 使用FFmpeg编码H264,再解码显示时,产生马赛克:有时是在画面静止时,静止时间越长,马赛克、伪影越多;有时是在画面切入切出时;有时是在网络带宽不够时 2、原因分析 2.1 丢帧 网络状况差的情况下(带宽不足),容易丢帧,在视频画面播放过程中,若I帧丢失了,…

LINUX内核升级

内核版本是2.6.18&#xff0c;新的内核是2.6.26。 1.下载新内核&#xff0c;下载网站www.kernel.org 2.copy内核到/usr/local/src下 3.解压内核 解压内核命令 tar -xjvf linux-2.6.26.tar.bz2 4.清理以前编译所生成的文件 命令为 make distclean&#xff0c;如果以前没有编…

我在美团的这两年,想和你分享

作者 | 石晓文来源 | 小小挖掘机&#xff08;ID&#xff1a;wAIsjwj&#xff09;012017.08.14&#xff0c;结束了两周的等待&#xff0c;如愿以偿开始了自己的美团实习生活&#xff0c;本来抱着三五个月走人&#xff0c;争取下一份实习的心态&#xff0c;没想到一直到转为暑期实…

【Qt】QtCreator updatePchInfo:switching to none

1、Pch名词解释 Pch(PreCompiled Headers)预编译头文件。 使用方法: CONFIG+=precompile_header PRECOMPILED_HEADER=XXX.h 2、updatePchInfo:switching to none 和QtCreator代码格式化Beautifier插件配置了clang code model有关系。 猜测:clang分析预编译头文件相关,…

学习框架、库的经验

熟悉基础语法把框架的功能过一遍&#xff0c;看看有哪些功能从文档的demo入口去学习上手会更快在你的开发目录要有一个专门写demo的页面&#xff0c;用以调试页面&#xff0c;试验新功能如果框架或者库提供有demo则更好&#xff0c;可以从中得到很多有用的东西

开源应用程序结构

2019独角兽企业重金招聘Python工程师标准>>> 给有兴趣的同学介绍的。这里面介绍了很多著名的开源软件的架构&#xff0c;相信读后会有所收获。 地址&#xff1a;http://www.aosabook.org/en/index.html 转载于:https://my.oschina.net/qinlinwang/blog/71649

免费GPU哪家强?谷歌Kaggle vs. Colab

作者 | Jeff Hale译者 | Monanfei责编 | 夕颜出品 | AI科技大本营&#xff08;id&#xff1a;rgznai100&#xff09;谷歌有两个平台提供免费的云端GPU&#xff1a;Colab和Kaggle&#xff0c; 如果你想深入学习人工智能和深度学习技术&#xff0c;那么这两款GPU将带给你很棒学习…

【SVN】svn“E155017工作副本的参考文件损坏、E200014文件校验和不匹配”的解决方法

1、问题描述 在执行svn提交时报错 svn: E155017: 工作副本的参考文件损坏 svn: E200014: ‘test.cpp’ 的文件校验和不匹配&#xff1a; 期望&#xff1a;xxxx 实际&#xff1a;xxxx 2、解决方法 2.1 拷贝 最好将提交的项目拷贝一份&#xff1b; 2.2 删除 使用svn rm --k…

QT程序启动加载流程简介

1. QT应用程序启动加载流程简介1.1 QWS与QPA启动客户端程序区别1.1.1 QWS(Qt Window System)介绍QWS(Qt Windows System)是QT自行开发的窗口系统&#xff0c;体系结构类似X Windows的C/S结构。QWS Server在物理设备上显示&#xff0c;QWS Client实现界面&#xff0c;两者…

QQ2012 Under Ubuntu

下载地址&#xff1a; QQ2012 Under Ubuntu转载于:https://www.cnblogs.com/ismdeep/archive/2012/08/09/2630067.html

25亿布局大湾区,创新工场的AI下一站

2019年6月5日&#xff0c;创新工场大湾区总部正式开业启动&#xff0c;集“产业投资AI 研究院商业赋能落地”三个功能为一体。当天创新工场还首次分享人工智能工程院成立两年来的成绩单&#xff0c;创新奇智的大湾区布局&#xff0c;并发布大湾区人才战略。创新工场也正式宣布第…

【TX2】TX2开发板系统默认串口有ttyS0(调试口)、ttyTHS1、ttyTHS2、ttyTHS3,通过修改设备树文件,可以新增三个串口

1、简述 TX2开发板系统默认串口有ttyS0&#xff08;调试口&#xff09;、ttyTHS1、ttyTHS2、ttyTHS3&#xff0c;通过修改设备树文件&#xff0c;可以新增三个串口。 2、设备树 设备树中关于串口部分的描述 2.1 基础配置 注意&#xff1a;在这里状态都配置成禁止&#xff…

unix的发展

转载http://blog.51cto.com/1193432/1671058转载于:https://www.cnblogs.com/vwei/p/9588823.html

让你的输入框使用Google云语音输入技术

2019独角兽企业重金招聘Python工程师标准>>> 只需一行代码&#xff0c;你的网站上面输入框&#xff08;input&#xff09;&#xff0c;直接可以在谷歌浏览器&#xff08;chrome&#xff09;上面使用Google的云语音输入技术。 在你的输入框input的HTML属性里面&#…

速度超Mask RCNN四倍,仅在单个GPU训练的实时实例分割算法 | 技术头条

作者 | Daniel Bolya Chong Zhou Fanyi Xiao Yong Jae Lee译者 | 刘畅责编 | Jane出品 | AI科技大本营&#xff08;id&#xff1a;rgznai100&#xff09;【导读】在论文《YOLACT&#xff1a;Real-time Instance Segmentation》中&#xff0c;作者提出了一种简洁的实时实例分割全…

JSP内置对象基础知识小结

JSP提供9大内置内象&#xff1a;一、request内象&#xff1a;封装了由客户端生成的HTTP请求的所有细节&#xff0c;主要包括了http头信息&#xff0c;系统信息&#xff0c;请求方式&#xff0c;请求参数等。1、获取访问请求参数&#xff1a;request.getParameter("arg&quo…

一个正执行的程序如何启动另一新程序并关闭现执行程序

最简单的方法有两个函数即可实现&#xff1a; //启动新程序WinExec("存放另一新程序的路径", SW_SHOW);//关闭现执行软件 ExitThread(0); 若在win ce 下&#xff0c;用WinExec这个函数就不对了&#xff0c;那时就应该用ShellExecuteEx了。 SHELLEXECUTEINFO ShExecIn…

【android】java.lang.NoClassDefFoundError或classnotfount等异常错误

在android上开发&#xff0c;当导入一个外部的包&#xff0c;可能会出现这类错误&#xff0c;我已经两次碰到了&#xff0c;一次是用科大讯飞的android开发包&#xff0c;另一次是用Jsoup包&#xff08;html 解析&#xff09;。 解决方案&#xff1a; 先去掉加入的外部包 不要把…

Java面试题(一)部分题目

博主马上要面对几家公司的面试&#xff0c;故自己准备了点面试题&#xff0c;仅供参考&#xff01; 1&#xff0c;线程的创建的方式&#xff1a;答&#xff1a;1,继承Thread(注意&#xff0c;此类其实也是实现了Runnable接口的)&#xff0c;2,实现Runnable接口2&#xff0c;1. …

在win ce中如何使正在运行的软件自动升级更新

创建两个独立的程序A和B&#xff1a;A是现正在运行的程序&#xff0c;B是用于辅助新版本的A覆盖旧版A 在客户端先运行A&#xff0c;使A提供从服务器端下载新版A放于一临时文件夹中&#xff0c;并运行B&#xff0c;关闭A&#xff1b; 运行的B用于执行&#xff1a;用新版A覆盖旧…

刘铁岩:AI打通关键环节,加快物流行业数字化转型

导语&#xff1a;近日&#xff0c;在微软亚洲研究院创新论坛上&#xff0c;微软亚洲研究院副院长刘铁岩分享了关于“AI物流”行业的实践经验。以下为其发言内容。 随着时代的发展&#xff0c;人工智能成为了决定性的技术&#xff0c;我们所谈的企业数字化转型也正在从“互联网”…

Unity的三种Interceptor

Unity默认提供了三种拦截器&#xff1a;TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。 TransparentProxyInterceptor&#xff1a;代理实现基于.NET Remoting技术&#xff0c;它可拦截对象的所有函数。缺点是被拦截类型必须派生于MarshalByRe…

Python编写循环的两个建议 | 鹅厂实战

作者 | piglei&#xff08;腾讯高级工程师&#xff09;转载自腾讯技术工程知乎专栏循环是一种常用的程序控制结构。我们常说&#xff0c;机器相比人类的最大优点之一&#xff0c;就是机器可以不眠不休的重复做某件事情&#xff0c;但人却不行。而“循环”&#xff0c;则是实现让…

作为JavaScript开发人员,这些必备的VS Code插件你都用过吗

本文翻译自&#xff1a;https://www.sitepoint.com/vs-code-extensions-java-developers/转载请注明出处&#xff1a;葡萄城官网&#xff0c;葡萄城为开发者提供专业的开发工具、解决方案和服务&#xff0c;赋能开发者。如今&#xff0c;Visual Studio Code无疑是最流行的轻量级…

matlab常遇小问题汇总

1、如何注释掉多行&#xff1a; 同时注释掉多行&#xff0c;有2种方法可行&#xff1a; (1)、选中所有要注释的行&#xff0c;按快捷键"Ctrl R" 或者 选择工具菜单"Text --> Comment"; 如果释放所有要注释的行&#xff0c;则按快捷键"Ctrl T&qu…

《几何与代数导引》习题1.35.4

求直线之间的距离$l_1:\frac{x1}{-1}\frac{y-1}{3}\frac{z5}{2}$.$l_2:\frac{x}{3}\frac{y-6}{-9}\frac{z5}{-6}$.解&#xff1a;点$q(-1,1,-5)$在直线$l_1$上&#xff0c;点$p(0,6,-5)$在直线$l_2$上.$\vec{pq}(-1,-5,0)$.直线$l_1$的方向向量为$(-1,3,2)$,直线$l_2$的方向向量…

深度学习难,这本书让你轻松学深度学习

深度学习在短短几年之内便让世界大吃一惊。它非常有力地推动了计算机视觉、自然语言处理、自动语音识别、强化学习和统计建模等多个领域的快速发展。随着这些领域的不断进步&#xff0c;人们现在可以制造自动驾驶的汽车&#xff0c;基于短信、邮件甚至电话的自动回复系统&#…