cat主要有三大功能:
1.一次顯示整個檔案。$ cat filename
2.從鍵盤建立一個檔案。$ cat > filename
隻能建立新檔案,不能編輯已有檔案.
3.将幾個檔案合并為一個檔案: $cat file1 file2 > file
參數:
-n 或 --number 由 1 開始對所有輸出的行數編号
-b 或 --number-nonblank 和 -n 相似,隻不過對于空白行不編号
-s 或 --squeeze-blank 當遇到有連續兩行以上的空白行,就代換為一行的空白行
-v 或 --show-nonprinting
例:
把 textfile1 的檔案内容加上行号後輸入 textfile2 這個檔案裡
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的檔案内容加上行号(空白行不加)之後将内容附加到 textfile3 裡。
cat -b textfile1 textfile2 >> textfile3
把 test.txt檔案扔進垃圾箱,賦空值test.txt
cat /dev/null > /etc/test.txt
cat 是一個文本檔案檢視和連接配接工具。檢視一個檔案的内容,用cat比較簡單,就是cat 後面直接接檔案名。
比如:
de>[root@localhost ~]# cat /etc/fstabde>
1.0 cat 文法結構;
de>cat [選項] [檔案]...de>
1.1 cat 檢視檔案内容執行個體;
de>[root@localhost ~]# cat /etc/profile 注:檢視/etc/目錄下的profile檔案内容;
[root@localhost ~]# cat -b /etc/fstab 注:檢視/etc/目錄下的profile内容,并且對非空白行進行編号,行号從1開始;
[root@localhost ~]# cat -n /etc/profile 注:對/etc目錄中的profile的所有的行(包括空白行)進行編号輸出顯示;
[root@localhost ~]# cat -e /etc/profile 注:檢視/etc/下的profile内容,并且在每行的結尾處附加$符号;de>
cat 加參數-n 和nl工具差不多,檔案内容輸出的同時,都會在每行前面加上行号;
de>[root@localhost ~]# cat -n /etc/profile
[root@localhost ~]# nl /etc/profilede>
cat 可以同時顯示多個檔案的内容,比如我們可以在一個cat指令上同時顯示兩個檔案的内容;
de>[root@localhost ~]# cat /etc/fstab /etc/profilede>
cat 對于内容極大的檔案來說,可以通過管道|傳送到more 工具,然後一頁一頁的檢視;
de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>
1.2 cat 的建立、連接配接檔案功能執行個體;
cat 有建立檔案的功能,建立檔案後,要以eof或stop結束;
de>[root@localhost ~]# cat > linuxsir.org.txt << eof 注:建立linuxsir.org.txt檔案;
> 我來測試 cat 建立檔案,并且為檔案輸入内容; 注:這是為linuxsir.org.txt檔案輸入内容;
> 北南南北 測試; 注:這是為linuxsir.org.txt檔案輸入内容;
> eof 注:退出編輯狀态;
[root@localhost ~]# cat linuxsir.org.txt 注:我們檢視一下linuxsir.org.txt檔案的内容;
我來測試 cat 建立檔案,并且為檔案輸入内容;
北南南北 測試;de>
cat 還有向已存在的檔案追加内容的功能;
de>[root@localhost ~]# cat linuxsir.txt 注:檢視已存在的檔案linuxsir.txt 内容;
i am beinannanbei from linuxsir.org . 注:内容行
我正在為cat指令寫文檔
[root@localhost ~]# cat >> linuxsir.txt << eof 注:我們向linuxsir.txt檔案追加内容;
> 我來測試cat向文檔追加内容的功能; 注:這是追回的内容
> ok?
> ok~
> 北南 呈上
> eof 注:以eof退出;
[root@localhost ~]# cat linuxsir.txt 注:檢視檔案内容,看是否追回成功。
i am beinannanbei from linuxsir.org .
我來測試cat向文檔追加内容的功能;
ok?
ok~
北南 呈上de>
cat 連接配接多個檔案的内容并且輸出到一個新檔案中;
假設我們有sir01.txt、sir02.tx和sir03.txt ,并且内容如下;
de>[root@localhost ~]# cat sir01.txt
123456
i am testing
[root@localhost ~]# cat sir02.txt
56789
beinan tested
[root@localhost ~]# cat sir03.txt
09876
linuxsir.org testingde>
我想通過cat 把sir01.txt、sir02.txt及sir03.txt 三個檔案連接配接在一起(也就是說把這三個檔案的内容都接在一起)并輸出到一個新的檔案sir04.txt 中。
注意:其原理是把三個檔案的内容連接配接起來,然後建立sir04.txt檔案,并且把幾個檔案的内容同時寫入sir04.txt中。特别值得一提的是,如果您輸入到一個已經存在的sir04.txt 檔案,會把sir04.txt内容清空。
de>[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt
[root@localhost ~]# more sir04.txt
cat 把一個或多個已存在的檔案内容,追加到一個已存在的檔案中
de>[root@localhost ~]# cat sir00.txt
linuxsir.org forever
[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt
[root@localhost ~]# cat sir00.txt
警告:我們要知道>意思是建立,>>是追加。千萬不要弄混了。造成失誤可不是鬧着玩的;