当要查看上千行的大文件时,我们可不会用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 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.

head命令的语法格式为:

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

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

实例:

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

新建test.txt,共14行.

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

使用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 last
K 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 last
K 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 +K
to 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 are
equivalent
-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=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to 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 or
becomes inaccessible; useful when following by
name, 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 at
least once every N seconds.
-v, --verbose always output headers giving file names
--help display this help and exit
--version output version information and exit If 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 c
2 d
3 e
4 f
5 g
6 h
7 i
8 j
9 k
10 l
11 m
12 n

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

  -c, --bytes=K            output the last K bytes; alternatively, use -c +K
to 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=N
with --follow=name, reopen a FILE which has not
changed size after N (default 5) iterations
to 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 or
becomes inaccessible; useful when following by
name, 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 at
least 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监控也关闭掉了。

linux shell 脚本攻略学习 -- head命令详解, tail命令详解的更多相关文章

  1. linux shell 脚本攻略学习20--awk命令入门详解

    awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...

  2. linux shell 脚本攻略学习14--head命令详解,tail命令详解

    当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...

  3. linux shell 脚本攻略学习18--grep命令详解

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是unix/linux中用于文本搜索 ...

  4. linux shell 脚本攻略学习16--wc命令详解,tree命令详解

    在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...

  5. linux shell 脚本攻略学习13--file命令详解,diff命令详解

    一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...

  6. Linux Shell脚本攻略学习总结:一

    终端打印 终端打印的常用命令有两个:echo和print 首先,我先介绍echo 1.echo echo这个命令接受三种形式的参数,实例如下: echo "Hello World" ...

  7. linux shell 脚本攻略学习6-xargs详解

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. 例如,下面的命令: rm `find /path -type f` ...

  8. linux shell 脚本攻略学习19--sed命令详解

    sed(意为流编辑器,英语“stream editor”的缩写)是Unix/linux常见的命令行程序.sed用来把文档或字符串里面的文字经过一系列编辑命令转换为另一种格式输出,即文本替换.sed通常 ...

  9. linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解

    文件权限详解 一.chmod命令详解 文件权限和所有权是Unix/Linux文件系统最显著的特征之一.linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种: 用户(User)是文 ...

随机推荐

  1. 字符编码:ASCII,Unicode和UTF-8

    字符编码是计算机技术的基石,想要熟练使用计算机,就必须懂得一点字符编码的知识. 1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两 ...

  2. LeetCode 675. Cut Off Trees for Golf Event

    原题链接在这里:https://leetcode.com/problems/cut-off-trees-for-golf-event/description/ 题目: You are asked to ...

  3. 【深度学习笔记】Anaconda及开发环境搭建

    在学习了一段时间台大李宏毅关于deep learning的课程,以及一些其他机器学习的书之后,终于打算开始动手进行一些实践了. 感觉保完研之后散养状态下,学习效率太低了,于是便想白天学习,晚上对白天学 ...

  4. PHP---如何修改域名的指定的根目录

    如何修改域名的指定的根目录 环境:linux 使用工具:xShell 修改域名指定的文件根目录需要修改nginx的配置文件 第一步:连接xShell 第二步:进入根路径找到nginx的配置文件 cd ...

  5. qqbot 配置

    qqbot 配置 用起来还是挺方便的,使用 pip install qqbot 就可以. 不过找配置文件没注意,以为是在程序目前,原来是在 C:\Users\xxx.qqbot-tmp 目录. 插件可 ...

  6. Oracle之 等待事件log file sync + log file parallel write (awr优化)

    这是3月份某客户的情况,原因是server硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们能够看到,该系统的load profile信息事实上并不高,每秒才21 ...

  7. Android 从上层到底层-----app层

    CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 MainActivity.java package com.aaron ...

  8. php 常用方法

    //返回json数据给js function json_output($err_code = 0 , $error_message = '' , $data = [] , $redirect = '' ...

  9. AT 指令和常见错误码

    一. 一般命令 1. AT+CGMI 给出模块厂商的标识. 2. AT+CGMM 获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800 或PCS 1900).当模块有多频带时,回 ...

  10. 关于NHibernate的一些代码

    SessionManager using System; using System.IO; using System.Runtime.Serialization; using System.Runti ...