您的位置:首页 > 运维架构 > Linux

linux下cat命令详解

2015-01-13 11:34 363 查看
本文转载自:http://www.cnblogs.com/perfy/archive/2012/07/23/2605550.html

简略版:

cat主要有三大功能:

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

2.从键盘创建一个文件。$ cat > filename

只能创建新文件,不能编辑已有文件.

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

参数:

-n 或 --number 由 1 开始对所有输出的行数编号

-b 或 --number-nonblank 和 -n 相似,只不过对于空白行不编号

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

-v 或 --show-nonprinting

例:

把 textfile1 的档案内容加上行号后输入 textfile2 这个档案里

cat -n textfile1 > textfile2

把 textfile1 和 textfile2 的档案内容加上行号(空白行不加)之后将内容附加到 textfile3 里。

cat -b textfile1 textfile2 >> textfile3



test.txt文件扔进垃圾箱,赋空值test.txt

cat /dev/null > /etc/test.txt







详细点的:

转:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html

cat 是一个文本文件查看和连接工具。查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名。

比如:

de>[root@localhost ~]# cat /etc/fstabde>

为了便于新手弟兄灵活掌握这个工具,我们多说一点常用的参数;

1.0 cat 语法结构;

de>cat [选项] [文件]...de>
选项
-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 显示此帮助信息并离开


1.1 cat 查看文件内容实例;

de>[root@localhost ~]# cat /etc/profile 注:查看/etc/目录下的profile文件内容;

[root@localhost ~]# cat -b /etc/fstab 注:查看/etc/目录下的profile内容,并且对非空白行进行编号,行号从1开始;

[root@localhost ~]# cat -n /etc/profile 注:对/etc目录中的profile的所有的行(包括空白行)进行编号输出显示;

[root@localhost ~]# cat -E /etc/profile 注:查看/etc/下的profile内容,并且在每行的结尾处附加$符号;de>

cat 加参数-n 和nl工具差不多,文件内容输出的同时,都会在每行前面加上行号;

de>[root@localhost ~]# cat -n /etc/profile

[root@localhost ~]# nl /etc/profilede>

cat 可以同时显示多个文件的内容,比如我们可以在一个cat命令上同时显示两个文件的内容;

de>[root@localhost ~]# cat /etc/fstab /etc/profilede>

cat 对于内容极大的文件来说,可以通过管道|传送到more 工具,然后一页一页的查看;

de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>

1.2 cat 的创建、连接文件功能实例;

cat 有创建文件的功能,创建文件后,要以EOF或STOP结束;

de>[root@localhost ~]# cat > linuxsir.org.txt << EOF 注:创建linuxsir.org.txt文件;

> 我来测试 cat 创建文件,并且为文件输入内容; 注:这是为linuxsir.org.txt文件输入内容;

> 北南南北 测试; 注:这是为linuxsir.org.txt文件输入内容;

> EOF 注:退出编辑状态;

[root@localhost ~]# cat linuxsir.org.txt 注:我们查看一下linuxsir.org.txt文件的内容;

我来测试 cat 创建文件,并且为文件输入内容;

北南南北 测试;de>

cat 还有向已存在的文件追加内容的功能;

de>[root@localhost ~]# cat linuxsir.txt 注:查看已存在的文件linuxsir.txt 内容;

I am BeiNanNanBei From LinuxSir.Org . 注:内容行

我正在为cat命令写文档

[root@localhost ~]# cat >> linuxsir.txt << EOF 注:我们向linuxsir.txt文件追加内容;

> 我来测试cat向文档追加内容的功能; 注:这是追回的内容

> OK?

> OK~

> 北南 呈上

> EOF 注:以EOF退出;

[root@localhost ~]# cat linuxsir.txt 注:查看文件内容,看是否追回成功。

I am BeiNanNanBei From LinuxSir.Org .

我正在为cat命令写文档

我来测试cat向文档追加内容的功能;

OK?

OK~

北南 呈上de>

cat 连接多个文件的内容并且输出到一个新文件中;

假设我们有sir01.txt、sir02.tx和sir03.txt ,并且内容如下;

de>[root@localhost ~]# cat sir01.txt

123456

i am testing

[root@localhost ~]# cat sir02.txt

56789

BeiNan Tested

[root@localhost ~]# cat sir03.txt

09876

linuxsir.org testingde>

我想通过cat 把sir01.txt、sir02.txt及sir03.txt 三个文件连接在一起(也就是说把这三个文件的内容都接在一起)并输出到一个新的文件sir04.txt 中。

注意:其原理是把三个文件的内容连接起来,然后创建sir04.txt文件,并且把几个文件的内容同时写入sir04.txt中。特别值得一提的是,如果您输入到一个已经存在的sir04.txt 文件,会把sir04.txt内容清空。

de>[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt

[root@localhost ~]# more sir04.txt

123456

i am testing

56789

BeiNan Tested

09876

linuxsir.org testingde>

cat 把一个或多个已存在的文件内容,追加到一个已存在的文件中

de>[root@localhost ~]# cat sir00.txt

linuxsir.org forever

[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt

[root@localhost ~]# cat sir00.txt

linuxsir.org forever

123456

i am testing

56789

BeiNan Tested

09876

linuxsir.org testingde>

警告:我们要知道>意思是创建,>>是追加。千万不要弄混了。造成失误可不是闹着玩的;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: