Linux文件目录操作命令-head命令、tail命令

一、head命令

headtail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示文件的开头至标准输出中,而 tail 就是看文件的结尾。

1.命令格式

head [参数]... [文件]...

2.命令功能

head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。

3.命令参数

  • -q 隐藏文件名
  • -v 显示文件名
  • -c<字节> 显示字节数
  • -n<行数> 显示的行数

4.使用实例

实例1:显示文件的前n行

命令:

head -n 5 log.log

演示操作及输出:

[tianyuanmengniu@localhost test]$ cat log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
this is line 22.

-----------------end
[tianyuanmengniu@localhost test]$ head -n 5 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

[tianyuanmengniu@localhost test]$

实例2:显示文件前n个字节

命令:

head -c 20 log.log

演示操作及输出:

[tianyuanmengniu@localhost test]$ head -c 20 log.log
this is line 1.
this[tianyuanmengniu@localhost test]$

实例3:文件的除了最后n个字节以外的内容

命令:

head -c -32 log.log

演示操作及输出:

[tianyuanmengniu@localhost test]$ head -c -32 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
[tianyuanmengniu@localhost test]$

实例4:输出文件除了最后n行的全部内容

命令:

head -n -6 log.log
```shell
演示操作及输出:
```shell
[tianyuanmengniu@localhost test]$ head -n -6 log.log
this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
[tianyuanmengniu@localhost test]$

二、tail命令

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

1.命令格式

tail[必要参数][选择参数][文件]

2.命令功能

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

3.命令参数

  • -f 循环读取
  • -q 不显示处理信息
  • -v 显示详细的处理信息
  • -c<数目> 显示的字节数
  • -n<行数> 显示行数
  • --pid=PID-f合用,表示在进程ID,PID死掉之后结束。
  • -q, --quiet, --silent 从不输出给出文件名的首部。
  • -s, --sleep-interval=S-f合用,表示在每次反复的间隔休眠S秒

4.使用实例

实例1:显示文件末尾内容

命令:

tail -n 5 log.log

演示执行及输出:

[tianyuanmengniu@localhost test]$ tail -n 5 log.log
this is line 20.
this is line 21.
this is line 22.

-----------------end
[tianyuanmengniu@localhost test]$

说明:显示文件最后5行内容

实例2:循环查看文件内容

命令:

tail -f ping.log

演示执行及输出:

[tianyuanmengniu@localhost test]$ ping www.baidu.com > ping.log &
[1] 2382
[tianyuanmengniu@localhost test]$ tail -f ping.log
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=9 ttl=57 time=13.1 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=10 ttl=57 time=14.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=11 ttl=57 time=13.0 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=12 ttl=57 time=16.0 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=13 ttl=57 time=12.9 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=14 ttl=57 time=14.5 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=15 ttl=57 time=13.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=16 ttl=57 time=13.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=17 ttl=57 time=12.9 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=18 ttl=57 time=13.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=19 ttl=57 time=12.8 ms

64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=20 ttl=57 time=12.4 ms

64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=21 ttl=57 time=11.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=22 ttl=57 time=13.1 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=23 ttl=57 time=12.5 ms
^C
[tianyuanmengniu@localhost test]$

说明:ping www.baidu.com > ping.log &,在后台ping远程主机。并输出文件到ping.log;这种做法也使用于一个以上的档案监视。用Ctrl+c来终止。

实例3:从第10行开始显示文件

命令:

tail -n +10 log.log

演示执行及输出:

[tianyuanmengniu@localhost test]$ tail -n +10 log.log
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
this is line 16.
this is line 17.
this is line 18.
this is line 19.
this is line 20.
this is line 21.
this is line 22.

-----------------end
[tianyuanmengniu@localhost test]$

你可能感兴趣的:(#,Linux文件目录操作命令,linux)