Linux基础命令-查看文本-CAT

cat

  • NAME(命令)
    cat - 连接文件和标准输出上打印
  • SYNOPSIS(简介)
    cat [OPTION]… [FILE]…
  • DESCRIPTION(描述)
    将文件或标准输入连接到标准输出。
   -A, --show-all
          等价于 -vET

   -b, --number-nonblank
          -n相似,对所有输出行编号,空白行不编号

   -e     等价于 -vE

   -E, --show-ends
          在每行的末尾显示 $

   -n, --number
         由1开始对所有输出行进行编号

   -s, --squeeze-blank
          阻止重复的空白输出行,有连续两行以上的空白
          行,就代换为一行的空白行

   -t     等价于 -vT
   
   -T, --show-tabs
          将 tab 字符显示为^I

   -u     (忽略)

   -v, --show-nonprinting
          使用 ^ 和 M-表示法, LFD 和 TAB除外
   如果没有指定文件,或者文件为"-",则从标准输入读取。
  • EXAMPLES(例子)
   cat f - g
   输出 f 的内容, 然后是标准输入, 然后是 g 的内容。
   cat    
   将标准输入复制到标准输出。
  • PRACTICE(实战练习)

  • 新建两个文件做cat练习

example1:

[root@localhost home]# vim example1
  1                             this is a study example1!
  2 usage:
  3     cat [option]... [file]...
  4 
  5 
  6     cat /home/example1 #查看整个文件
  7 
  8 
  9     cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开>    始
 10 
 11 
 12     cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编
    号,行号从1开始
 13 
 14 
 15     cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
 16 
 17 
 18     cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
                                                                                              

example2:

[root@localhost home]# vim example2
  1                               this is a study example2!
  2 usage:                       
  3     cat [option]... [file]...
  4     
  5 
  6     cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2    内容  7 
  8 
  9     cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的e    xample2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看 10 
 11 
 12     cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的e    xample2内容通过管道传给more命令按行查看内容,空格键向下翻行,q键退出查看
  1. cat 查看文件内容实例

  2. cat /home/example1 # 查看/home/目录下的example1文件所有内容

[root@localhost home]# cat /home/example1
							this is a study example1!
usage:
	cat [option]... [file]...


	cat /home/example1 #查看整个文件


	cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  


	cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始


	cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


	cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
  1. cat -b /home/example1 # 查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始
[root@localhost home]# cat -b /home/example1
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...


     4		cat /home/example1 #查看整个文件


     5		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  


     6		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始


     7		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


     8		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行

  1. cat -n /home/example1 # 对/home目录中的example1的所有的行(包括空白行)进行编号输出显示
[root@localhost home]# cat -n /home/example1
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...
     4	
     5	
     6		cat /home/example1 #查看整个文件
     7	
     8	
     9		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
    10	
    11	
    12		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    13	
    14	
    15		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    16	
    17	
    18		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行

  1. cat -E /home/example1 # 查看/home/下的example1内容,并且在每行的结尾处附加$符号
[root@localhost home]# cat -E /home/example1
							this is a study example1!$
usage:$
	cat [option]... [file]...$
$
$
	cat /home/example1 #查看整个文件$
$
$
	cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  $
$
$
	cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始$
$
$
	cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号$
$
$
	cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行$

  1. cat -sn /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行,并且对所有行编号
[root@localhost home]# cat -sn /home/example1
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...
     4	
     5		cat /home/example1 #查看整个文件
     6	
     7		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
     8	
     9		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    10	
    11		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    12	
    13		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
  1. cat /home/example1 /home/example2 # cat 可以同时显示多个文件的内容,我们可以在一个cat命令上同时显示两个文件的内容
[root@localhost home]# cat /home/example1 /home/example2
							this is a study example1!
usage:
	cat [option]... [file]...


	cat /home/example1 #查看整个文件


	cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  


	cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始


	cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


	cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
                              this is a study example2!
usage:
	cat [option]... [file]...
   
   
	cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容


	cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看


	cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看





  1. cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看
[root@localhost home]# cat /home/example1 /home/example2 | more
							this is a study example1!
usage:
	cat [option]... [file]...


	cat /home/example1 #查看整个文件


	cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开
  


	cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编
号,行号从1开始


	cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


	cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
                              this is a study example2!
usage:
	cat [option]... [file]...
   
   
	cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2
内容


	cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的e
xample2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看


	cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的e
xample2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看





  1. cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看
[root@localhost home]# cat /home/example1 /home/example2 | less
this is a study example1!
usage:
        cat [option]... [file]...


        cat /home/example1 #查看整个文件


        cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开
始  


        cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始


        cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


        cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
                              this is a study example2!
usage:
        cat [option]... [file]...
   
   
        cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容


        cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的e
xample2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看


        cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的e
xample2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看





  1. cat创建一个文件实例
  2. cat /home/example1 /home/example2 >/etc/example3 # 将/home/下的example1内容连接到/home/下的example2,输入到新建文本/home/下的example3
[root@localhost home]# cat /home/example3
cat: /home/example3: 没有那个文件或目录
[root@localhost home]# cat /home/example1 /home/example2 >/home/example3
[root@localhost home]# cat -n /home/example3
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...
     4	
     5	
     6		cat /home/example1 #查看整个文件
     7	
     8	
     9		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
    10	
    11	
    12		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    13	
    14	
    15		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    16	
    17	
    18		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
    19	                              this is a study example2!
    20	usage:
    21		cat [option]... [file]...
    22	   
    23	   
    24		cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容
    25	
    26	
    27		cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看
    28	
    29	
    30		cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看
    31	
    32	
    33	
    34	

  1. cat /home/example1 /home/example2 >>/etc/example # 将/home/下的example1内容连接到/home/下的example2,追加到文本/home/下的example3
[root@localhost home]# vim /home/example3
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...
     4	
     5	
     6		cat /home/example1 #查看整个文件
     7	
     8	
     9		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
    10	
    11	
    12		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    13	
    14	
    15		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    16	
    17	
    18		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
    19	                              this is a study example2!
    20	usage:
    21		cat [option]... [file]...
    22	   
    23	   
    24		cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容
    25	
    26	
    27		cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看
    28	
    29	
    30		cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看
    31	
    32	
    33	
    34	这个是example3文件  

[root@localhost home]# cat /home/example1 /home/example2 >>/home/example3
[root@localhost home]# cat -n /home/example3
     1								this is a study example1!
     2	usage:
     3		cat [option]... [file]...
     4	
     5	
     6		cat /home/example1 #查看整个文件
     7	
     8	
     9		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
    10	
    11	
    12		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    13	
    14	
    15		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    16	
    17	
    18		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
    19	                              this is a study example2!
    20	usage:
    21		cat [option]... [file]...
    22	   
    23	   
    24		cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容
    25	
    26	
    27		cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看
    28	
    29	
    30		cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看
    31	
    32	
    33	
    34	这个是example3文件
    35	
    36								this is a study example1!
    37	usage:
    38		cat [option]... [file]...
    39	
    40	
    41		cat /home/example1 #查看整个文件
    42	
    43	
    44		cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  
    45	
    46	
    47		cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始
    48	
    49	
    50		cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号
    51	
    52	
    53		cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行
    54	                              this is a study example2!
    55	usage:
    56		cat [option]... [file]...
    57	   
    58	   
    59		cat /home/example1 /home/example2 #查看/home/目录下的example1内容和/home/目录下的example2内容
    60	
    61	
    62		cat /home/example1 /home/example2 | more #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给more命令按页查看内容,空格键向下翻页,q键退出查看
    63	
    64	
    65		cat /home/example1 /home/example2 | less #查看/home/目录下的example1内容和/home/目录下的example2内容通过管道传给less命令按行查看内容,j键向下翻行k键向上翻行,q键退出查看
    66	
    67	
    68	
    69	

注意:>,>>的区别。

①:>的意思是新建一个文档。如果没有文档则创建文档将>左边的文挡内容粘贴到文档内;如果文档已经创建文档内有内容,则将>左边的文档内容覆盖到右边的文档当中。

②:>>的意思是在文档内容末尾追加内容。将>>左边的文档内容复制到>>右边这个文档中,文档有内容则在文档最末行粘贴保存

  1. 把example3文件扔进垃圾箱,赋空值example3
[root@localhost home]# cat /dev/null >/home/example3
[root@localhost home]# cat /home/example3
[root@localhost home]# ls
example1  example2  example3  install.log

命令延伸:

  • tac
  • tac命令跟cat命令相反,cat命令是从第一行向下打印,tac则是从最后一行向上打印
[root@localhost home]# tac /home/example1
	cat -s /home/example1 #阻止重复的空白输出行,有连续两行以上的空白行,就代换为一行的空白行


	cat -E /home/example1 #查看/home/目录下的example1内容,并且在每行的结尾处附加$符号


	cat -n /home/example1 #查看/home/目录下的example1内容,并且对所有的行(包括非空白行)进行编号,行号从1开始


	cat -b /home/example1 #查看/home/目录下的example1内容,并且对非空白行进行编号,行号从1开始  


	cat /home/example1 #查看整个文件


	cat [option]... [file]...
usage:
							this is a study example1!

  • more
  • more命令是分页向下翻页
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | more

  • less
  • less命令是按行向下翻行
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | less

  • head
  • head命令是显示文档头部内容
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | head

  • tail
  • tail命令是显示文档尾部内容
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | tail
  • find
  • 查找文件所在目录
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | find example*
example1
example2
example3
  • grep
  • 搜索文本内容
[root@localhost home]# cat /home/example1 /home/example2 /home/example3 | grep file

猜你喜欢

转载自blog.csdn.net/stjas/article/details/83786576