Linux常用基本命令(cat)

cat命令

作用:连接多个文件并且打印到屏幕输出,或者重定向到其他文件,也可以用来查看显示单个文件,或者多个文件。

格式:

cat [option] [file]

1,最简单的用法,直接跟文件名称,查看文件内容

ghostwu@dev:~/linux/cat$ ls
ghostwu@dev:~/linux/cat$ echo 'hello,my name is ghostwu, how are you?' > test.txt
ghostwu@dev:~/linux/cat$ cat test.txt 
hello,my name is ghostwu, how are you?
ghostwu@dev:~/linux/cat$ 

2,也可以使用如下方式,向文件写入或者追加内容

ghostwu@dev:~/linux/cat$ ls
test.txt
ghostwu@dev:~/linux/cat$ cat >> test.txt << EOF
> 这是我新增的内容
> 这是第三行内容
> EOF
ghostwu@dev:~/linux/cat$ cat test.txt 
hello,my name is ghostwu, how are you?
这是我新增的内容
这是第三行内容

3,-n与-b   都是对文件进行编号,-b不会对空行编号

ghostwu@dev:~/linux/cat$ cat test.txt 
hello,my name is ghostwu, how are you?
这是我新增的内容
这是第三行内容
ghostwu@dev:~/linux/cat$ cat >> test.txt << EOF
> 
> 
> 上面加入了两个空行
> EOF
ghostwu@dev:~/linux/cat$ cat test.txt 
hello,my name is ghostwu, how are you?
这是我新增的内容
这是第三行内容


上面加入了两个空行
ghostwu@dev:~/linux/cat$ cat -n test.txt 
     1    hello,my name is ghostwu, how are you?
     2    这是我新增的内容
     3    这是第三行内容
     4    
     5    
     6    上面加入了两个空行
ghostwu@dev:~/linux/cat$ cat -b test.txt 
     1    hello,my name is ghostwu, how are you?
     2    这是我新增的内容
     3    这是第三行内容


     4    上面加入了两个空行
ghostwu@dev:~/linux/cat$ 

4,-E 在每一行的行尾显示美元符号

ghostwu@dev:~/linux/cat$ cat -E test.txt 
hello,my name is ghostwu, how are you?$
这是我新增的内容$
这是第三行内容$
$
$
上面加入了两个空行$

5,-s: 把两个以上连续的空行,变成一个

ghostwu@dev:~/linux/cat$ cat -n test.txt 
     1    hello,my name is ghostwu, how are you?
     2    这是我新增的内容
     3    这是第三行内容
     4    
     5    
     6    
     7    
     8    上面加入了两个空行
     9    
    10    上面加入了一个空行
ghostwu@dev:~/linux/cat$ cat -ns test.txt 
     1    hello,my name is ghostwu, how are you?
     2    这是我新增的内容
     3    这是第三行内容
     4    
     5    上面加入了两个空行
     6    
     7    上面加入了一个空行

6,利用/dev/null 删除文件内容

ghostwu@dev:~/linux/cat$ cat test.txt 
hello,my name is ghostwu, how are you?
这是我新增的内容
这是第三行内容




上面加入了两个空行

上面加入了一个空行
ghostwu@dev:~/linux/cat$ cat /dev/null > test.txt
ghostwu@dev:~/linux/cat$ cat test.txt 

7,利用重定向写入内容

ghostwu@dev:~/linux/cat$ cat test.txt 
ghostwu@dev:~/linux/cat$ cat > test.txt
this is ghostwu 
how are you
ghostwu@dev:~/linux/cat$ cat test.txt 
this is ghostwu
how are you

内容输入完毕,用ctrl+d或者ctrl+c中断输入

8,显示多个文件内容

ghostwu@dev:~/linux/cat$ cat > abc.txt
this is abc.txt
ghostwu@dev:~/linux/cat$ cat test.txt abc.txt 
this is ghostwu
how are you
this is abc.txt

 

posted @ 2018-05-17 06:34  ghostwu  阅读(2374)  评论(0编辑  收藏  举报
Copyright ©2017 ghostwu