linux之cat命令详解
簡略版:
cat主要有三大功能:1.一次顯示整個(gè)文件。$ cat?filename
2.從鍵盤創(chuàng)建一個(gè)文件。$ cat?>?filename??
?? 只能創(chuàng)建新文件,不能編輯已有文件.
3.將幾個(gè)文件合并為一個(gè)文件: $cat?file1?file2 > file 參數(shù):
-n 或 --number 由 1 開始對所有輸出的行數(shù)編號(hào)
-b 或 --number-nonblank 和 -n 相似,只不過對于空白行不編號(hào)
-s 或 --squeeze-blank 當(dāng)遇到有連續(xù)兩行以上的空白行,就代換為一行的空白行
-v 或 --show-nonprinting
例:
把 textfile1 的檔案內(nèi)容加上行號(hào)后輸入 textfile2 這個(gè)檔案里
cat -n textfile1 > textfile2
把 textfile1 和 textfile2 的檔案內(nèi)容加上行號(hào)(空白行不加)之后將內(nèi)容附加到 textfile3 里。
cat -b textfile1 textfile2 >> textfile3 把test.txt文件扔進(jìn)垃圾箱,賦空值test.txt
cat /dev/null > /etc/test.txt ? ? ? ?
詳細(xì)點(diǎn)的:
轉(zhuǎn):http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html
cat 是一個(gè)文本文件查看和連接工具。查看一個(gè)文件的內(nèi)容,用cat比較簡單,就是cat 后面直接接文件名。
比如:
de>[root@localhost ~]# cat /etc/fstabde>為了便于新手弟兄靈活掌握這個(gè)工具,我們多說一點(diǎn)常用的參數(shù);
1.0 cat 語法結(jié)構(gòu);
-A, --show-all 等價(jià)于 -vET
-b, --number-nonblank 對非空輸出行編號(hào)
-e 等價(jià)于 -vE
-E, --show-ends 在每行結(jié)束處顯示 $
-n, --number 對輸出的所有行編號(hào)
-s, --squeeze-blank 不輸出多行空行
-t 與 -vT 等價(jià)
-T, --show-tabs 將跳 字符顯示為 ^I
-u (被忽略)
-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外
--help 顯示此幫助信息并離開
1.1 cat 查看文件內(nèi)容實(shí)例;
[root@localhost ~]# cat -b /etc/fstab???注:查看/etc/目錄下的profile內(nèi)容,并且對非空白行進(jìn)行編號(hào),行號(hào)從1開始;
[root@localhost ~]# cat -n /etc/profile????注:對/etc目錄中的profile的所有的行(包括空白行)進(jìn)行編號(hào)輸出顯示;
[root@localhost ~]# cat??-E /etc/profile?????注:查看/etc/下的profile內(nèi)容,并且在每行的結(jié)尾處附加$符號(hào);de>
cat 加參數(shù)-n 和nl工具差不多,文件內(nèi)容輸出的同時(shí),都會(huì)在每行前面加上行號(hào);
de>[root@localhost ~]# cat -n /etc/profile[root@localhost ~]# nl??/etc/profilede>
cat 可以同時(shí)顯示多個(gè)文件的內(nèi)容,比如我們可以在一個(gè)cat命令上同時(shí)顯示兩個(gè)文件的內(nèi)容;
de>[root@localhost ~]# cat /etc/fstab /etc/profilede>cat 對于內(nèi)容極大的文件來說,可以通過管道|傳送到more 工具,然后一頁一頁的查看;
de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>
1.2 cat 的創(chuàng)建、連接文件功能實(shí)例;
cat 有創(chuàng)建文件的功能,創(chuàng)建文件后,要以EOF或STOP結(jié)束;
> 我來測試 cat 創(chuàng)建文件,并且為文件輸入內(nèi)容;???????注:這是為linuxsir.org.txt文件輸入內(nèi)容;
> 北南南北 測試;???????????????????注:這是為linuxsir.org.txt文件輸入內(nèi)容;
> EOF???注:退出編輯狀態(tài);
[root@localhost ~]# cat linuxsir.org.txt??注:我們查看一下linuxsir.org.txt文件的內(nèi)容;
我來測試 cat 創(chuàng)建文件,并且為文件輸入內(nèi)容;
北南南北 測試;de>
cat 還有向已存在的文件追加內(nèi)容的功能;
de>[root@localhost ~]# cat??linuxsir.txt 注:查看已存在的文件linuxsir.txt 內(nèi)容;I am BeiNanNanBei From LinuxSir.Org .????注:內(nèi)容行
我正在為cat命令寫文檔
[root@localhost ~]# cat >> linuxsir.txt << EOF???注:我們向linuxsir.txt文件追加內(nèi)容;
> 我來測試cat向文檔追加內(nèi)容的功能;???????注:這是追回的內(nèi)容
> OK?
> OK~
> 北南 呈上
> EOF???注:以EOF退出;
[root@localhost ~]# cat linuxsir.txt??注:查看文件內(nèi)容,看是否追回成功。
I am BeiNanNanBei From LinuxSir.Org .
我正在為cat命令寫文檔
我來測試cat向文檔追加內(nèi)容的功能;??
OK?
OK~
北南 呈上de>
cat 連接多個(gè)文件的內(nèi)容并且輸出到一個(gè)新文件中;
假設(shè)我們有sir01.txt、sir02.tx和sir03.txt ,并且內(nèi)容如下;
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 三個(gè)文件連接在一起(也就是說把這三個(gè)文件的內(nèi)容都接在一起)并輸出到一個(gè)新的文件sir04.txt 中。
注意:其原理是把三個(gè)文件的內(nèi)容連接起來,然后創(chuàng)建sir04.txt文件,并且把幾個(gè)文件的內(nèi)容同時(shí)寫入sir04.txt中。特別值得一提的是,如果您輸入到一個(gè)已經(jīng)存在的sir04.txt 文件,會(huì)把sir04.txt內(nèi)容清空。
de>[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt[root@localhost ~]# more sir04.txt
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testingde>
cat 把一個(gè)或多個(gè)已存在的文件內(nèi)容,追加到一個(gè)已存在的文件中
de>[root@localhost ~]# cat sir00.txtlinuxsir.org forever
[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt
[root@localhost ~]# cat sir00.txt
linuxsir.org forever
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testingde> 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的linux之cat命令详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux之tmp文件夹
- 下一篇: linux之universal usb