每天2个Linux命令 tail head


2017-07-19 每天2个Linux命令 tail命令

tail命令用于输入文件中的尾部内容。tail命令默认在屏幕上显示指定文件的末尾10行。

如果给定的文件不止一个,则在显示的每个文件前面加一个文件名标题。


(1)用法:

用法:   tail [必要参数] [选择参数]   [文件]   

          如果没有指定文件或者文件名为“-”,则读取标准输入。

(2)功能:

功能:  输出文件的末尾部分

(3)选项参数:

1) -n <k行数>                                     显示文件末尾k行内容

2) -c <k字节数>                                  显示文件末尾k个字节数

3) -f                   循环读取                

4) -q                  不显示处理信息

5) -v                  显示详细的处理信息

(4)实例:

  1)[root@localhost Documents]# tail -n 5 ./tail_text 查看文件后5行的内容

          复制代码
        [root@localhost Documents]# cat tail_text
        > 01 the first line!
        > 02 the second line!
        > 03 the third line!
        > 04 the forth line!
        > 05 the fifth line!
        > 06 the sixth line!
        > o7 the seventh line!
        > 08 the eighth line!
        > 09 the nineth line!
        > 10 the tenth line!
        > 11 the eleven line!
        > 12 the twelve line!
        [root@localhost Documents]# tail -n 5 ./tail_text
        > 08 the eighth line!
        > 09 the nineth line!
        > 10 the tenth line!
        > 11 the eleven line!
        > 12 the twelve line!
        复制代码

   等价于tail -5 text_tail 查看后5行的内容

        [root@localhost Documents]# tail -5 tail_text
        > 08 the eighth line!
        > 09 the nineth line!
        > 10 the tenth line!
        > 11 the eleven line!
        > 12 the twelve line!

    2)[root@localhost Documents]# tail -n +5 tail_text             从第5行开始显示

    复制代码
    [root@localhost Documents]# tail -n +5 tail_text
    > 05 the fifth line!
    > 06 the sixth line!
    > o7 the seventh line!
    > 08 the eighth line!
    > 09 the nineth line!
     10 the tenth line!
    > 11 the eleven line!
    > 12 the twelve line!

3)[root@localhost Documents]# head -n -5 tail_text与[root@localhost Documents]# tail -n -5 tail_text

复制代码
[root@localhost Documents]# head -n 5 tail_text          //显示文件前5行的内容
01 the first line!
02 the second line!
03 the third line!
04 the forth line!
05 the fifth line!
[root@localhost Documents]# head -n -5 tail_text          //除了文件后五行全部显示
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!                                          //head命令的-n参数,当后面的整数为正为负是有区别的
[root@localhost Documents]# tail -n 5 tail_text                 //tail命令的-n参数,当后面的整数为正为负是一样的
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
[root@localhost Documents]# tail -n -5 tail_text               //都是显示末尾的整数的绝对值行
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
复制代码

4)[root@localhost Documents]# tail -c 30 tail_text                     显示末尾的字节数

复制代码
[root@localhost Documents]# tail -c 30 tail_text
n line!
 12 the twelve line!
[root@localhost Documents]# tail -c -30 tail_text
n line!
> 12 the twelve line!
[root@localhost Documents]# head -c 30 tail_text
> 01 the first line!
> 02 the [root@localhost Documents]# head -c -30 tail_text
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleve[root@localhost Documents]# 

5)[root@localhost Documents]# tail -f tail_text               循环读取内容输出到标准输出

复制代码
[root@localhost Documents]# tail -f tail_text                             //默认是后10行
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
^C
[root@localhost Documents]# tail -f -n 12 tail_text   //也可以自己指定
> 01 the first line!
> 02 the second line!
    > 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
^Z
[6]+  已停止               tail -f -n 12 tail_text
[root@localhost Documents]# tail -f -n 7 tail_text
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
复制代码
  当然,也可以把信息输入到文件中:

复制代码
[root@localhost Documents]# tail -f tail_text>tempory
    ^Z
[9]+  已停止               tail -f tail_text > tempory
[root@localhost Documents]# cat tempory
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!

6)[root@localhost Documents]# tail -n +5 tail_text与[root@localhost Documents]# tail -n 5 tail_text

复制代码
[root@localhost Documents]# tail -n +5 tail_text
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
>
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
>
> 12 the twelve line!
[root@localhost Documents]# tail -n 5 tail_text
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
> 11 the eleven line!
 12 the twelve line!
[root@localhost Documents]# head -n +5 tail_text
 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
[root@localhost Documents]# head -n 5 tail_text
> 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!

7)[root@localhost Documents]# tail -n +10 tail_text |head -n -2

复制代码
[root@localhost Documents]# tail -n +10 tail_text       //从第10行显示到尾部
> 10 the tenth line!
> 11 the eleven line!
> 12 the twelve line!
[root@localhost Documents]# head -n -2 tail_text       //除了末尾两行之外前面的都显示
 01 the first line!
> 02 the second line!
> 03 the third line!
> 04 the forth line!
> 05 the fifth line!
> 06 the sixth line!
> o7 the seventh line!
> 08 the eighth line!
> 09 the nineth line!
> 10 the tenth line!
[root@localhost Documents]# tail -n +10 tail_text |head -n -2   
//综合起来,用管道命令就是后一个命令处理前面的结果,因此达到只显示第10行的效果
> 10 the tenth line!
[root@localhost Documents]# 

2017-07-19 每天2个Linux命令 head命令

head命令用于显示文件的开头的内容。在默认情况下,head命令显示文件的头10行内容。

如果指定了多于一个文件,在每一段输出前会给出文件名作为文件头。

如果不指定文件,或者文件为”-“,则从标准输入读取数据。


(1)用法:

用法: head [选项]... [文件]...

(2)功能:

将每个指定文件的头10 行显示到标准输出。

(3)选项参数:

1)-q             隐藏文件名

2)-v             显示文件名

3)-c<字节>          显示字节数

4)-n<行数>          显示的行数

(4)实例:

  1)[root@localhost Documents]# head head_text            默认显示文件的前10行

    复制代码
    [root@localhost Documents]# ll
    总用量 12
    -rw-r--r--. 1 root root 664 5月   9 07:59 head_text
    -rw-r--r--. 1 root root  45 5月   9 08:15 less1
    -rw-r--r--. 1 root root  57 5月   9 08:16 less2
    [root@localhost Documents]# head head_text
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

2)[root@localhost Documents]# head -5 head_text  与   
 [root@localhost Documents]# head -n 5 head_text             相同的功能:自定义显示文件前5行

复制代码
[root@localhost Documents]# head -5 head_text
I am studing orders of Linux!
I am trying to write as many as lines of text!


No matter how low you consider yourself,
[root@localhost Documents]# head -n 5 head_text
I am studing orders of Linux!
I am trying to write as many as lines of text!


No matter how low you consider yourself,

3)[root@localhost Documents]# head -c 20 head_text    指定自定义显示前20个字节的内容

[root@localhost Documents]# head -c 20 head_text
I am studing orders [root@localhost Documents]# 
[root@localhost Documents]# head -c 60 head_text
I am studing orders of Linux!
I am trying to write as many a

4)[root@localhost Documents]# head -c -50 head_text           指定除末尾的50个字节外全部显示

复制代码
[root@localhost Documents]# head -c -50 head_text
I am studing orders of Linux!
I am trying to write as many as lines of text!


No matter how low you consider yourself,
there is always someone looking behind you,
hoping that they were that high!

Something you want keep all the time,always you will lose!

Never forget to say "thanks"!

Hppay today,also,prepared for happiness in the future!

Don't aim your success if you want it,just do what you love and believe and finally you will success!

Maybe you can be laze man like a pig,but you can't feel free as it!

I am a college school student!
I am planning to live and work in hangzhou or guangzhou!
I am from[root@localhost Documents]# 

5)I am from[root@localhost Documents]# head -n -10 head_text    除最后10行外全部显示

复制代码
I am from[root@localhost Documents]# head -n -10 head_text
I am studing orders of Linux!
I am trying to write as many as lines of text!


No matter how low you consider yourself,
there is always someone looking behind you,
hoping that they were that high!

Something you want keep all the time,always you will lose!

Never forget to say "thanks"!

[root@localhost Documents]# 

6)[root@localhost Documents]# head -v less1 less2  
显示多个文件,并且在显示前打印出每个文件的文件名

复制代码
[root@localhost Documents]# head -v  less1 less2
==> less1 <==
Lost means Get!

No losing No getting!

End!

==> less2 <==
If you want keep,you always lose!

Certainly It is!
End!


-v参数是默认的,即是不加也会如此:

复制代码
[root@localhost Documents]# head less1 less2
==> less1 <==
Lost means Get!

No losing No getting!

End!

==> less2 <==
If you want keep,you always lose!

Certainly It is!
End!

7)[root@localhost Documents]# head -q -n 3 less1 less2 head_text             
 -q参数用来指定显示多个文件,不加文件名。同时也可以用-n 10指定只显示前3行

复制代码
[root@localhost Documents]# head -q -n 3 less1 less2 head_text
Lost means Get!

No losing No getting!
If you want keep,you always lose!

Certainly It is!
I am studing orders of Linux!
I am trying to write as many as lines of text!

[root@localhost Documents]# 

8)[root@localhost Documents]# head --help

复制代码
[root@localhost Documents]# 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'

9)[root@localhost Documents]# head --version

复制代码
[root@localhost Documents]# 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 编写。

文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录