【Linux文件管理命令cat用法】

[cat]

1:查看文件内容 (cat + 文件名)

root@sb:~#cat s.txt

~~~~~

~~~~~

root@sb:~#

2:创建文件(cat + >文件名  + <<结束符标志)

root@sb:~#cat >s.txt << OFF

>>OFF

root@sb:~#

若没有此文件,将在当前目录下创建s.txt,若有,则替换s.txt文件,内容清空。

3:查找文件中的内容(cat + 文件名  +  | grep + 查找内容)

root@sb:~#cat  s.txt  | grep hello

printf("hello world");

root@sb:~#

4:向文件中写入内容(cat + >文件名 + <<结束符 或者 cat + >>文件名 + <<结束符)

root@sb:~#cat >s.txt <<OFF

>>hello world

>>thanks

>>OFF

root@sb:~#cat  s.txt

hello world

thanks

root@sb:~#cat >>s.txt <<OFF

>>i am a hero

>>OFF

root@sb:~#cat s.txt

hello world

thanks

i am a hero

root@sb:~#

cat >>s.txt <<OFF为向文件的最后一行追加内容

5:把一个文件的内容写入到另一个文件(cat + 源文件名 + > +  要写入的文件名)

root@sb:~#cat  s.txt  > t.txt

root@sb:~#cat t.txt

hello world

thanks

i am a hero

root@sb:~#

6:把两个文件的内容合并到另一个文件(cat + 源文件1名 + 源文件2名 + > > +  要写入的文件名)

root@sb:~#cat  s.txt   t.txt  >> h.txt

root@sb:~#

备注:

语法格式

cat [-AbeEnstTuv] [--help] [--version] fileName

参数说明

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

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

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

-v 或 --show-nonprinting

 


版权声明:本文为qq_35866271原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。