linux 命令详解

本文主要内容来自Linux man 手册

命令名称:

cat ( concatenate )连接文件并打印到标准输出

命令用法:

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

命令概述:

连接多个文件到标准输出。
如果没有文件,或者文件为 - ,则从标准输入读取数据。

命令参数:

-A , --show-all : 显示全部相当于 -vET,即将 -v -E -T 等参数同时用上-b , --number-nonblank : 显示非空行的行号给非空行标号,与 -n 相反-e   相当于 -vE ,即同时使用 -v 和 -E 两个参数-E,--show-ends在每行的末尾显示 $ 符号-n,--number给所有输出行标号-s,--squeeze-blank 不显示重复的空行,即多行空格只显示成一行-t   相当于 -vT ,即同时使用 -v 和 -T-T,--show-tabs将 TAB 字符显示为 ^I-u   (忽略),可能作为预留参数,或该参数已取消-v,--show-nonprinting : 显示不可打印字符,如转义字符使用 ^ 和 M- 记号,除了 LFD (可能就是 $ ) 和 TAB ( ^I )--help 显示帮助信息--version 显示版本信息

示例:

1. cat [文本文件]

不带任何参数,cat 命令可以用来打印文本

xiaohui@ubuntu:~/work/dir1$ cat a.c
cat test#include <stdio.h>int main(int argc,char **argv)
{printf("hello world\n");printf("hello linux\n");printf("hello shell\n");printf("hello cat\n");return 0;
}
xiaohui@ubuntu:~/work/dir1$ 

2. cat -n

参数 n 的作用是在打印内容的每行行首加上了行号。

xiaohui@ubuntu:~/work/dir1$ cat a.c -n1 cat test2   3   #include <stdio.h>4   5   int main(int argc,char **argv)6 {7      printf("hello world\n");8     printf("hello linux\n");9     printf("hello shell\n");10        printf("hello cat\n");11      return 0;12 }
xiaohui@ubuntu:~/work/dir1$ 

3. cat -T

参数 T 会将打印内容里的 TAB 键显示为 ^I 符号

xiaohui@ubuntu:~/work/dir1$ cat -T a.c
cat test#include <stdio.h>int main(int argc,char **argv)
{
^Iprintf("hello world\n");
^Iprintf("hello linux\n");
^Iprintf("hello shell\n");
^Iprintf("hello cat\n");
^Ireturn 0;
}
xiaohui@ubuntu:~/work/dir1$ 

4. cat -E

参数 E 在打印的内容里每行的行尾加了一个 $ 符号。

xiaohui@ubuntu:~/work/dir1$ cat -E a.c
cat test$
$
#include <stdio.h>$
$
int main(int argc,char **argv)$
{$printf("hello world\n");$printf("hello linux\n");$printf("hello shell\n");$printf("hello cat\n");$return 0;$
}$
xiaohui@ubuntu:~/work/dir1$ 

5. cat -b

参数 b 的作用和参数 n 相似,只是参数 b 不显示空行的行号。

xiaohui@ubuntu:~/work/dir1$ cat -b a.c1 cat test2   #include <stdio.h>3   int main(int argc,char **argv)4 {5      printf("hello world\n");6     printf("hello linux\n");7     printf("hello shell\n");8     printf("hello cat\n");9       return 0;10 }
xiaohui@ubuntu:~/work/dir1$

6. cat -s

参数 s 的作用是压缩空行,如果文本中有两行或以上的空行,在用 cat 命令之后将只显示一行空行。

首先是不使用 s 参数:
xiaohui@ubuntu:~/work/dir1$ cat a.c -n1 cat test2   3   #include <stdio.h>4   5   6   7   int main(int argc,char **argv)8 {9  10  11      printf("hello world\n");12        printf("hello linux\n");13        printf("hello shell\n");14        printf("hello cat\n");15      return 0;16     17  }
xiaohui@ubuntu:~/work/dir1$ 
以下为使用 s 参数的效果
xiaohui@ubuntu:~/work/dir1$ cat a.c -ns1    cat test2   3   #include <stdio.h>4   5   int main(int argc,char **argv)6 {7  8       printf("hello world\n");9     printf("hello linux\n");10        printf("hello shell\n");11        printf("hello cat\n");12      return 0;13     14  }
xiaohui@ubuntu:~/work/dir1$ 

很显然如果文本有很多连续空行,加 s 参数使打印的内容更加紧凑。

7. cat -v

如果文本文件是在windows下创建或文本格式被设置为dos,有时我们会看到一些 ^M符号,其实它就相当于 \r,只是不同的文件格式的转义字符也有所不同。
参数 v 可以让隐藏的转义字符也打印中显示。

xiaohui@ubuntu:~/work/dir1$ cat -v a.c
cat test^M
^M
#include <stdio.h>^M
^M
int main(int argc,char **argv)^M
{^Mprintf("hello world\n");^Mprintf("hello linux\n");^Mprintf("hello shell\n");^Mprintf("hello cat\n");^Mreturn 0;^M
^M
}^M
xiaohui@ubuntu:~/work/dir1$ 

8. cat -A

还有一些参数是带有复合功能,如 -e 代表 -vE 、-t 代表 -vT、-A 代表 -vET

xiaohui@ubuntu:~/work/dir1$ cat -A a.c
cat test^M$
^M$
#include <stdio.h>^M$
^M$
int main(int argc,char **argv)^M$
{^M$
^Iprintf("hello world\n");^M$
^Iprintf("hello linux\n");^M$
^Iprintf("hello shell\n");^M$
^Iprintf("hello cat\n");^M$
^Ireturn 0;^M$
^M$
}^M$
xiaohui@ubuntu:~/work/dir1$ 

9. cat 或 cat -

仅使用 cat 或使用 cat - 时,是向标准输出打印数据,输入什么就打印什么。

xiaohui@ubuntu:~/work/dir1$ cat -
cat - test
cat - test
hello world >>>>
hello world >>>>
^C
xiaohui@ubuntu:~/work/dir1$ 

10. cat 与 > / >> 搭配使用(1)

但是我们并不常会向标准输出打印数据,如果使用输出从定向符 > 或追加符 >>,就可以实现向一个文件写数据。

cat > [文本文件] 或 cat >> [文本文件]

用 cat + >/>> 来创建一个新文件:

xiaohui@ubuntu:~/work/dir1$ cat > tmp.c
new file's name is "tmp.c"cat test2020-12-31
xiaohui@ubuntu:~/work/dir1$

如果想结束写入,使用 Ctrl + DCtrl + C,建议使用前者,Ctrl + C 会造成最后一行数据的丢失,除非你在结束前先输个回车。
新文件的内容:

xiaohui@ubuntu:~/work/dir1$ cat tmp.c
new file's name is "tmp.c"cat test2020-12-31
xiaohui@ubuntu:~/work/dir1$

使用 > 还是 >> 需要根据实际情况决定,两者区别是 > 会将原来文本的内容覆盖!

cat >/>>  [文本文件] << [结束标志]

如果嫌 Ctrl + D 按起来麻烦,我们也可以手动规定文本输入的结束标志,当输入这个标志,即表示退出输入。

xiaohui@ubuntu:~/work/dir1$ cat > tmp2.c << end
> cat test for manual stop
> the new file name is tmp2.c
> 16:06
> end
xiaohui@ubuntu:~/work/dir1$ 

文本内容:

xiaohui@ubuntu:~/work/dir1$ cat tmp2.c
cat test for manual stop
the new file name is tmp2.c
16:06
xiaohui@ubuntu:~/work/dir1$ 

11. cat 与 > / >> 搭配使用(2)

cat [文本文件 1 ]... >/>> [文本文件 2 ]

除了手动输入字符串到文本文件,我们还可以用 cat 实现文件的连接。
连接前两个文件的内容:

xiaohui@ubuntu:~/work/dir1/dir2$ cat a.c
this is file 1 ,my name is a.c
end of a.c
xiaohui@ubuntu:~/work/dir1/dir2$ cat b.c
this is file 2 ,my name is b.c
end of b.c
xiaohui@ubuntu:~/work/dir1/dir2$ cat a.c >> b.c

使用 cat 连接后(使用 >> ),文件1 的内容追加到了文件2的末尾。

xiaohui@ubuntu:~/work/dir1/dir2$ cat b.c
this is file 2 ,my name is b.c
end of b.c
this is file 1 ,my name is a.c
end of a.c
xiaohui@ubuntu:~/work/dir1/dir2$ 
cat [ 文本文件1 ] [ 文本文件2 ] ... >/>> [ 文本文件3 ]

用该方法可以将多个文件合并后连接到在一个文件里
连接前3个文件的内容:

xiaohui@ubuntu:~/work/dir1/dir2$ cat a.c
this is file 1 ,my name is a.c
end of a.c
xiaohui@ubuntu:~/work/dir1/dir2$ cat b.c
this is file 2 ,my name is b.c
end of b.c
xiaohui@ubuntu:~/work/dir1/dir2$ cat c.c
this is file 3 ,my name is c.c
end of c.c
xiaohui@ubuntu:~/work/dir1/dir2$ cat a.c b.c >> c.c

连接后的 文本文件3:

xiaohui@ubuntu:~/work/dir1/dir2$ cat c.c
this is file 3 ,my name is c.c
end of c.c
this is file 1 ,my name is a.c
end of a.c
this is file 2 ,my name is b.c
end of b.c
xiaohui@ubuntu:~/work/dir1/dir2$ 

man手册

以下为 cat 命令手册原文:

CAT(1)                           User Commands                          CAT(1)NAMEcat - concatenate files and print on the standard outputSYNOPSIScat [OPTION]... [FILE]...DESCRIPTIONConcatenate FILE(s) to standard output.With no FILE, or when FILE is -, read standard input.-A, --show-allequivalent to -vET-b, --number-nonblanknumber nonempty output lines, overrides -n-e     equivalent to -vE-E, --show-endsdisplay $ at end of each line-n, --numbernumber all output lines-s, --squeeze-blanksuppress repeated empty output lines-t     equivalent to -vT-T, --show-tabsdisplay TAB characters as ^I-u     (ignored)-v, --show-nonprintinguse ^ and M- notation, except for LFD and TAB--help display this help and exit--versionoutput version information and exitEXAMPLEScat f - gOutput f's contents, then standard input, then g's contents.cat    Copy standard input to standard output.AUTHORWritten by Torbjorn Granlund and Richard M. Stallman.REPORTING BUGSGNU coreutils online help: <http://www.gnu.org/software/coreutils/>Report cat translation bugs to <http://translationproject.org/team/>COPYRIGHTCopyright  ©  2016  Free Software Foundation, Inc.  License GPLv3+: GNUGPL version 3 or later <http://gnu.org/licenses/gpl.html>.This is free software: you are free  to  change  and  redistribute  it.There is NO WARRANTY, to the extent permitted by law.SEE ALSOtac(1)Full documentation at: <http://www.gnu.org/software/coreutils/cat>or available locally via: info '(coreutils) cat invocation'GNU coreutils 8.25               February 2017                          CAT(1)

Linux命令详解之 cat相关推荐

  1. c linux time微秒_学习linux,看这篇1.5w多字的linux命令详解(6小时讲明白Linux)

    用心分享,共同成长 没有什么比每天进步一点点更重要了 本篇文章主要讲解了一些linux常用命令,主要讲解模式是,命令介绍.命令参数格式.命令参数.命令常用参数示例.由于linux命令较多,我还特意选了 ...

  2. Linux命令详解之 ls

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: ls ( list files / list directory contents )列举目录内容 命令用法: ls [选项 ...

  3. Linux命令详解之 head和tail

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: head 输出文件的开头部分 tai 输出文件的结尾部分 命令用法: head/tail [选项]... [文件]... [ ...

  4. linux命令详解--eval

    linux命令详解--eval shell中的eval 功能说明:重新运算求出参数的内容. 语 法:eval [参数] 补充说明:eval可读取一连串的参数,然后再依参数本身的特性来执行. 参 数:参 ...

  5. 《Linux命令详解手册》——Linux畅销书作家又一力作

    关注IT,更要关心IT人,让系统管理员以及程序员工作得更加轻松和快乐.鉴于此, 图灵公司引进了国外知名出版社John Wiley and Sons出版的Fedora Linux Toolbox: 10 ...

  6. linux下载命令 scp,linux命令详解之scp命令

    作用 scp命令常用于linux之间复制文件和目录. scp是secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 格式 从本地复制到远程 复制文件 sc ...

  7. linux中date使用方法,linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)...

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例) 发布于 2016-02-07 15:58:40 | 108 次阅读 | 评论: 0 | 来源: 网友投递 LinuxLinux是一 ...

  8. RAR for Linux 命令详解

    RAR for Linux 命令详解 用法:  rar <命令>-<开关 1> -<开关 N> <压缩文件> <文件...> <@列表 ...

  9. Linux命令详解之 mv

    linux 命令详解 本文主要内容来自Linux man 手册 命令名称: mv(move)移动/重命名文件 命令用法: mv [选项]... [-T] 源文件 目标文件 mv [选项]... 源文件 ...

  10. Linux命令详解之w命令

    Linux命令详解之w命令 1.命令详解 ··· NAMEw - Show who is logged on and what they are doing. w命令就是用来展示谁在登录,以及他们在做 ...

最新文章

  1. 遗传算法(Genetic Algorithm )+C++实现解决TSP问题
  2. Python 的电子邮件编程
  3. 接到需求之后,产品经理如何高效的从“想”到“做”?
  4. python 画图_用python解九宫格以及画图
  5. 在状态栏中显示鼠标位置坐标
  6. [20151014]关于result cache.txt
  7. Linux-服务管理命令chkconfig
  8. android h5 指定浏览器_微信h5网页如何实现跳转到手机默认浏览器
  9. 后端实践:Nginx日志配置(超详细)
  10. 控制极限(UCL,LCL) 和规格极限(USL,LSL)
  11. 无需转化直接使用ESD映像文件安装系统简明教程
  12. Alpha选股:资本资产定价模型(CAPM)
  13. Android 线程4件套 MessageQueue Message Looper Handler之Looper
  14. echarts的饼图制作分析
  15. php 刀客友朋,说好的英雄拯救世界
  16. 青玉案.元夕-2023
  17. 传感器实验——寻迹模块
  18. Winform的内容
  19. php新浪微博 登录接口文档,qq登录,新浪微博登录接口申请过程中遇到的问题
  20. jq点击下载word

热门文章

  1. ip类「ABCDE五类」区分和私有ip地址的知识
  2. 简易中控紫猫插件版(2)基本思路介绍
  3. 有限自动机DFA 、 无限自动机NFA
  4. 【很赞的一片文章】android获取手机号码(主要是移动手机)
  5. css3 - 图标元素动画效果1 - 只执行一次动画
  6. 日语词频分析——mecab使用
  7. 加密与启示录:Crypto是流着奶与蜜的“应许之地”
  8. 微信小程序金额千分位
  9. 初级、中级和高级开发人员之间有什么区别?
  10. 期货专业术语中英文对照