cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令。

注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。

    (1)用法:

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

    (2)功能:

将[文件]或标准输入组合输出到标准输出。

    (3)选项参数:

      1)-n, --number                                    对输出的所有行编号

2) -s, --squeeze-blank                           不输出多行空行,有连续两行以上的空白行,就代换为一行的空白行

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

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

5) -A, --show-all                                   等价于 -vET,显示不可打印字符,行尾显示“$”

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

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

8) --help                                              显示此帮助信息并退出

9) --version                                          输出版本信息并退出

    (4)实例:

        由于cat命令是查看文档的,所以首先新建文本文档test1.txt,test2.txt,test3.txt并在文档中写入内容:

方法一:

(1)首先用touch指令新建三个文档:

[sunjimeng@localhost Document]$ touch {text1.txt,text2.txt,text3.txt}
[sunjimeng@localhost Document]$ ll
总用量
-rw-rw-r--. sunjimeng sunjimeng 5月 : text1.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text2.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text3.txt

(2)用图形界面,打开文档输入数据:

(3)由于在CentOs里文档有自动备份的功能,因此这里有6个文档。其中带~符号的需要用查看备份的软件来打开:

(4)查看shell中的文档信息:

[sunjimeng@localhost Document]$ ll
总用量
-rw-rw-r--. sunjimeng sunjimeng 5月 : text1.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text1.txt~
-rw-rw-r--. sunjimeng sunjimeng 5月 : text2.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text2.txt~
-rw-rw-r--. sunjimeng sunjimeng 5月 : text3.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text3.txt~

方法二:在shell中直接修改文档的内容:

[sunjimeng@localhost Document]$ touch text4.txt
[sunjimeng@localhost Document]$ cat >text4.txt <<EOF
> test4's first line;
> test4's second line;
> test4's third line;
> EOF
[sunjimeng@localhost Document]$ ll
总用量
-rw-rw-r--. sunjimeng sunjimeng 5月 : text1.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text1.txt~
-rw-rw-r--. sunjimeng sunjimeng 5月 : text2.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text2.txt~
-rw-rw-r--. sunjimeng sunjimeng 5月 : text3.txt
-rw-rw-r--. sunjimeng sunjimeng 5月 : text3.txt~
-rw-rw-r--. sunjimeng sunjimeng 5月 : text4.txt //这里并没有创建备份文件,是区别所在
[sunjimeng@localhost Document]$

1)[sunjimeng@localhost Document]$ cat -n text4.txt                 将包括空行在内的各行按编号输出

[sunjimeng@localhost Document]$ cat >text4.txt <<EOF                   //先修改text4.txt的内容
> text4's first line
>
>
> text4's second line
>
> text4's third line
>
>
> EOF
[sunjimeng@localhost Document]$ cat -n text4.txt
text4's first line text4's second line text4's third line

2)[sunjimeng@localhost Document]$ cat -b text4.txt               将除空行在内的各行按编号输出

[sunjimeng@localhost Document]$ cat -b text4.txt
text4's first line text4's second line text4's third line

3)[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt        用cat命令直接输出各个文件,可以是一个也可以是多个

[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt
test1's first line;
test1's second line;
test1's third line;
test2's first line;
test2's second line;
test2's third line;
test3's first line;
test3's second line;
test3's third line;

4)[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt             将讲text1.txt和text2.txt输出到text5.txt里,和输出到标准输出一样,也可以有-n,-b等参数

由于这个特性,cat命令可以将多个压缩包合并成一个,可以用tar命令解压

# cat test.tar.gz_?? > test.tar.gz #可以用cat命令将被切割的多个压缩包合并成一个
# tar -xvzf test.tar.gz #再用tar命令解压
[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt
[sunjimeng@localhost Document]$ cat text5.txt
test1's first line;
test1's second line;
test1's third line;
test2's first line;
test2's second line;
test2's third line;
[sunjimeng@localhost Document]$

5)[sunjimeng@localhost Document]$ tac text5.txt                  倒序输出文件的各行内容

[sunjimeng@localhost Document]$ tac text5.txt
test2's third line;
test2's second line;
test2's first line;
test1's third line;
test1's second line;
test1's first line;

6)[sunjimeng@localhost Document]$ cat -s text4.txt                    输出文档中的内容,如果有多个空行则用一个代替

[sunjimeng@localhost Document]$ cat -s text4.txt                最多连续输出一个空行
text4's first line text4's second line text4's third line [sunjimeng@localhost Document]$ cat text4.txt 有多少空行,输出多少空行
text4's first line text4's second line text4's third line

7)[sunjimeng@localhost Document]$ cat >text6.txt                      从键盘录入内容到文件,回车是保存,退出Ctrl+z

[sunjimeng@localhost Document]$ cat >text6.txt
I am MenAngel! //除了最后一个回车之后,其余回车是文档中数据的换行并保存
Practice Order!
^Z //回车后是Ctrl+Z命令退出
[]+ 已停止 cat > text6.txt
[sunjimeng@localhost Document]$ cat text6.txt
I am MenAngel!
Practice Order!

8)[sunjimeng@localhost Document]$ cat -E text4.txt                    输出各行文本,并且以$符结尾

[sunjimeng@localhost Document]$ cat -E text4.txt
text4's first line$
$
$
text4's second line$
$
text4's third line$
$
$

9)[sunjimeng@localhost Document]$ cat >text6.txt <<EOF              用$取表达式的值小小范例:

[sunjimeng@localhost Document]$ cat >text6.txt <<EOF
> pwd=$(pwd)
> EOF
[sunjimeng@localhost Document]$ cat text6.txt
pwd=/home/sunjimeng/Document

10)[sunjimeng@localhost Document]$ cat --help

[sunjimeng@localhost Document]$ cat --help
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。 -A, --show-all 等于-vET
-b, --number-nonblank 对非空输出行编号
-e 等于-vE
-E, --show-ends 在每行结束处显示"$"
-n, --number 对输出的所有行编号
-s, --squeeze-blank 不输出多行空行
-t 与-vT 等价
-T, --show-tabs 将跳格字符显示为^I
-u (被忽略)
-v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
--help 显示此帮助信息并退出
--version 显示版本信息并退出 如果没有指定文件,或者文件为"-",则从标准输入读取。 示例:
cat f - g 先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
cat 将标准输入的内容复制到标准输出。 GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误
要获取完整文档,请运行:info coreutils 'cat invocation'

11)[sunjimeng@localhost Document]$ cat --version

[sunjimeng@localhost Document]$ cat --version
cat (GNU coreutils) 8.22
Copyright (C) Free Software Foundation, Inc.
许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
本软件是自由软件:您可以自由修改和重新发布它。
在法律范围内没有其他保证。 由Torbjörn Granlund 和Richard M. Stallman 编写。

每天一个Linux命令(8)cat命令的更多相关文章

  1. Linux命令学习-cat命令

    Linux中,cat命令的全称是concatenate,主要用于显示文件内容. 查看centos系统版本 cat /etc/centos-release 查看文件 gogs.log 的内容 cat g ...

  2. 每天一个linux命令:cat 命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  3. linux常用命令:cat 命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: cat [选项] [文件] ...

  4. 全网最详细的Linux命令系列-cat命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 命令格式: cat [选项] [文件].. ...

  5. linux常用命令(8)cat命令

    cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1 命令格式:cat [选项] [文件]. ...

  6. linux命令之------Cat命令

    Cat命令 作用:cat命令用于连接文件并打印,查看文件内容: -n或--number:由1开始对所有输出的行数编号: -b或--number-nonblank:和-n相似,只不过对于空白行不做编号: ...

  7. 【Linux常见命令】cat命令

    cat - concatenate files and print on the standard output cat 命令用于连接文件并打印到标准输出设备上. 用法: 1. cat file 查看 ...

  8. 【Linux】使用cat命令创建文本文件

    在Linux界面输入 Linux:/usr/test # cat >test01.sh 接着按回车,输入内容:"echo hello world !" 回车后按 ctrl+d ...

  9. Centos文件切割利器_split命令及cat命令合并文件

    有个文件要处理,因为很大,所以想把它切成若干份,每份N行,以便并行处理.split命令可以将一个大文件分割成很多个小文件,有时需要将文件分割成更小的片段,为提高可读性,生成日志等 命令格式 -b:值为 ...

  10. Linux命令:cat命令详解

    概述:查看文件内容,连接文件,重定向输出到文件 1.查看整个文件 2.cat > filename 创建文件 3.合并输出到文件 1.查看文件(单个或者多个) cat demo.txt 2.创建 ...

随机推荐

  1. spring mvc controller中的异常封装

    http://abc08010051.iteye.com/blog/2031992 一直以来都在用spring mvc做mvc框架,我使用的不是基于注解的,还是使用的基于xml的,在controlle ...

  2. sqlserver 字段内容做in条件 列变成行显示

    sqlserver中 字段内容做in条件用到方法:CHARINDEX(value,situation) 列变行显示用到:stuff 详情自行查找. 例子: stuff((select ','+name ...

  3. nginx实现某个页面http访问,其余全部跳转到https

    全站https实现某个页面可以http访问,其余全部跳转到https,注意下面的location,如果不加root 配置 找不到这个文件的server { listen ; server_name w ...

  4. Python基础--通用序列操作

    Python 继续 Python包含6种内建的序列,各自是:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.我们将逐步进行介绍. 今天主要介绍一下通用序列操作.放之四海而 ...

  5. reveal end of document

     window - Preferences - Run/Debug - Console 将 Console buffer size (characters)设置大一点

  6. android开发系列之aidl

    aidl在android开发中的主要作用就是跨进程通讯来着,说到进程相比很多人都是非常熟悉了,但是为什么会有跨进程通讯这个概念呢?原来在android系统中,有这么一套安全机制,为了各个Apk数据的独 ...

  7. u3D大场景的优化

    首先介绍下draw call(这个东西越少你的游戏跑的越快): 在游戏中每一个被展示的独立的部分都被放在了一个特别的包中,我们称之为“描绘指令”(draw call),然后这个包传递到3D部分在屏幕上 ...

  8. notepad 替换行收尾字符串或在行首尾新增字符

    用 Notepad++ 打开,把每一个将要放在表中单元格的内容放一行(注: ^ 代表行首 $ 代表行尾) 去除行尾空格和空白行:按CTRL+H 选择正则表达式-- 查找目标:\s+$ 替换为空 去除行 ...

  9. DirectShow使用心得

    用了3天时间,将DShow加入到了游戏中,记录下心得,方便要使用的童鞋以及以后的自己查看.1. Video Mixing Renderer 9,内部使用Direct3D 9,需要Windows XP或 ...

  10. [译]GLUT教程 - 每秒帧数

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> Frames per Second 你的程序实际上跑得多快? 有时我们 ...