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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM