Shell中常常会用到cat命令。可是总是不是特别清楚:

cat命令的用途是连接文件或标准输入并打印。

这个命令经常使用来显示文件内容。或者将几个文件连接起来显示。或者从标准输入读取内容并显示,它常与重定向符号配合使用。

1.命令格式:

cat [选项] [文件]...

2.命令功能:

cat主要有三大功能:

1.一次显示整个文件:cat filename

2.从键盘创建一个文件:cat > filename 仅仅能创建新文件,不能编辑已有文件.

3.将几个文件合并为一个文件:cat file1 file2 > file

3.命令參数:

-A, --show-all           等价于 -vET

-b, --number-nonblank    对非空输出行编号

-e                       等价于 -vE

-E, --show-ends          在每行结束处显示 $

-n, --number     对输出的全部行编号,由1開始对全部输出的行数编号

-s, --squeeze-blank  有连续两行以上的空白行,就代换为一行的空白行

-t                       与 -vT 等价

-T, --show-tabs          将跳格字符显示为 ^I

-u                       (被忽略)

-v, --show-nonprinting   使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

实例一:把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件中

命令:

cat -n log2012.log log2013.log    #直接输出到后面一个文件里

输出:

[root@localhost test]# cat log2012.log

2012-01

2012-02

======[root@localhost test]# cat log2013.log

2013-01

2013-02

2013-03

======[root@localhost test]# cat -n log2012.log log2013.log

1  2012-01

2  2012-02

3

4

5  ======

6  2013-01

7  2013-02

8

9

10  2013-03

11  ======[root@localhost test]#

说明:

实例二:把 log2012.log 和 log2013.log 的文件内容加上行号(空白行不加)之后将内容附加到 log.log 里。 

命令:

cat -b log2012.log log2013.log log.log

输出:

[root@localhost test]# cat -b log2012.log log2013.log log.log

1  2012-01

2  2012-02

3  ======

4  2013-01

5  2013-02

6  2013-03

7  ======[root@localhost test]#

实例三:把 log2012.log 的文件内容加上行号后输入 log.log 这个文件中 

命令:

输出:

[root@localhost test]# cat log.log

[root@localhost test]# cat -n log2012.log > log.log

[root@localhost test]# cat -n log.log

1  2012-01

2  2012-02

3

4

5  ======

[root@localhost test]#

实例四:使用here doc来生成文件

输出:

[root@localhost test]# cat >log.txt <<EOF
#用到了EOF。表示假设碰到EOF。那么就表示输入结束       并且这里是先cat>log.txt表示新建一个文件

> Hello

> World

> Linux

> PWD=$(pwd)

> EOF

[root@localhost test]# ls -l log.txt

-rw-r--r-- 1 root root 37 10-28 17:07 log.txt

[root@localhost test]# cat log.txt

Hello

World

Linux

PWD=/opt/soft/test

[root@localhost test]#

tac (反向列示)

命令:

tac log.txt

输出:

[root@localhost test]# tac log.txt

PWD=/opt/soft/test

Linux

World

Hello

说明:

tac 是将 cat 反写过来。所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在萤幕上,而 tac 则是由最后一行到第一行反向在萤幕上显示出来!

以下開始介绍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.使用实例:

显示文件末尾内容

命令:

tail -n 5 log2014.log  #(直接表示显示最后5行)

输出:

[root@localhost test]# tail -n 5 log2014.log

2014-09

2014-10

2014-11

2014-12

==============================[root@localhost test]#

说明:

显示文件最后5行内容



循环查看文件内容

命令:

tail -f test.log

输出:

[root@localhost ~]# ping 192.168.120.204 > test.log &

[1] 11891[root@localhost ~]# tail -f test.log
                 #在不停地ping的过程中,循环显示

PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.

64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms

64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms

64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms

64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms

64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms

64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms

64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms

64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms

64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms

[root@localhost ~]#

说明:

ping 192.168.120.204 > test.log & //在后台ping远程主机。

并输出文件到test.log;这样的做法也使用于一个以上的档案监视。用Ctrl+c来终止。

从第5行開始显示文件

命令:

tail -n +5 log2014.log

输出:

[root@localhost test]# cat log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[root@localhost test]# tail -n +5 log2014.log

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================


说明: -n 5 表示最后5行的数据。 -n +5表示正数的低5行開始

都已经说到tail,那么就应该顺带着将head也复习了

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

1.命令格式:

head [參数]... [文件]...

2.命令功能:

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

3.命令參数:

-q 隐藏文件名称

-v 显示文件名称

-c<字节> 显示字节数

-n<行数> 显示的行数

4.使用实例:



   显示文件的前n行

命令:

head -n 5 log2014.log

输出:

[root@localhost test]# cat log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12

==============================

[root@localhost test]# head -n 5 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05[root@localhost test]#



显示文件前n个字节

命令:

head -c 20 log2014.log           #n 表示行,c 表示的是字节数

输出:

[root@localhost test]# head -c 20 log2014.log

2014-01

2014-02

2014

[root@localhost test]#



文件的除了最后n个字节以外的内容

命令:

head -c -32 log2014.log

输出:

[root@localhost test]# head -c -32 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07

2014-08

2014-09

2014-10

2014-11

2014-12[root@localhost test]#



输出文件除了最后n行的所有内容

命令:

head -n -6 log2014.log

输出:

[root@localhost test]# head -n -6 log2014.log

2014-01

2014-02

2014-03

2014-04

2014-05

2014-06

2014-07[root@localhost test]#


说明:假设须要取反的话,那么就在-n -c  之后的数字上加上“-”号


版权声明:本文博客原创文章,博客,未经同意,不得转载。

每日回顾Shell —cat,tail,head的更多相关文章

  1. lunix cat tail more等用法

    cat主要有三大功能:  1.一次显示整个文件.  $ cat filename  2.从键盘创建一个文件.  $ cat > filename  只能创建新文件,不能编辑已有文件.  3.将几 ...

  2. 【原创】用python实现shell的tail操作

    在工作过程中发现监控实时刷新文件时,不是那么的任性. 故结合shell中的tail,做了一个类似tail的python脚本. 详情如下: #!/usr/bin/env python #coding=u ...

  3. shell cat EOF 变量自动解析问题

    使用如下shell安装node时,一直提示 command not found wget https://mirrors.huaweicloud.com/nodejs/latest-v8.x/node ...

  4. shell cat 合并文件,合并数据库sql文件

    > 覆盖写入 >> append模式写入 ###################################################################合并数 ...

  5. Linux之cat tail less常见用法

    1.cat 通常查找出错误日志 cat error.log | grep 'foo' , 这时候我们还有个需求就是输出当前这个日志的前后几行: cat error.log | grep -C 10 ' ...

  6. linux shell cat 命令

    cat:查看文件的内容.连接文件.创建一个或多个文件和重定向输出到终端或文件  用法:cat [选项] [文件] 1. $ cat hello.txt 显示hello.txt文本文件中的内容 2. $ ...

  7. linux每日命令(15):tail命令

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

  8. shell head tail

    head:  打印文件的前10行(默认) tail:     打印文件的后10行(默认) 如果你是个典型的开发或者部署人员,是不是觉得开始亲切起来.我们平时用的最多的命令 $tail -f  cata ...

  9. shell cat 用法

    cat命令的用途是连接文件或标准输入并打印. 1.命令格式:cat [选项] [文件]... 2.命令功能:cat主要有三大功能:1.一次显示整个文件:cat filename   --常用来显示文件 ...

随机推荐

  1. 5款帮助简化的HTML5 Audio开发的Javascript类库

    HTML5的audio标签提供了我们方便控制声音的功能,可是使用原生的HTML5来开发声音或者音乐相关的项目仍旧很的麻烦.在今天这篇文章中,我们将介绍5款帮助你简化开发的javascript audi ...

  2. Hacker News网站的文章排名算法工作原理

    In this post I'll try to explain how Hacker News ranking algorithm works and how you can reuse it in ...

  3. delphi 网页提交按钮执行点击事件

    遍历即可实现,下列代码仅供参考: var i: integer; T: OleVariant; begin T := WebBrowser1.Document; do begin if T.all.i ...

  4. 打开excel2007文档时显示“excel无法打开文件~$book.xltm”

    此问题的出现是因为意外情况导致Excel临时文件没有删掉,把C:\Program Files\Microsoft Office\Office12\XLSTART里面的临时文件“~$book”给删除就好 ...

  5. adb 之android的神器am

    am命令,am全称activity manager,你能使用am去模拟各种系统的行为,例如去启动一个activity,强制停止进程,发送广播进程,修改设备屏幕属性等等 命令窗口通过adb shell ...

  6. LCD显示--Ht1621b芯片显示屏驱动

    Ht1621b芯片显示屏驱动 关于HT1621b芯片的具体信息能够參考数据手冊上的内容:百度文库HT1621b中文资料 CS : 片选输入接一上拉电阻当/CS 为高电平读写HT1621的数据和命令无效 ...

  7. Face-landmarks-detection-benchmark 人脸特征定位网站汇总

    源地址:https://www.douban.com/note/525032729/   https://github.com/delphifirst/FaceXhttps://github.com/ ...

  8. Ubuntu下is not in the sudoers file 问题解决

    在Ubuntu12.04 下,使用sudo apt-get install XXX 时,突然跳出 username is not in the sudoers file的问题 然后我一查此userna ...

  9. [Cocos2d-x学习笔记]Android NDK: Host 'awk' tool is outdated. Please define NDK_HOST_AWK to point to Gawk or Nawk解决方案

    Android NDK: Host 'awk' tool is outdated. Please define NDK_HOST_AWK to point to Gawk or Nawkawk过期网上 ...

  10. ORACLE常用数据库类型(转)

    oracle常用数据类型 1.Char 定长格式字符串,在数据库中存储时不足位数填补空格,它的声明方式如下CHAR(L),L为字符串长度,缺省为1,作为变量最大32767个字符,作为数据存储在ORAC ...