Linux中的cat命令用法
linux中的cat有三種用法:
-列舉所有文件
-創(chuàng)建文件
-合并文件
詳細(xì)請(qǐng)看:
http://www.cnblogs.com/perfy/archive/2012/07/23/2605550.html
linux下cat命令詳解
簡(jiǎn)略版:
cat主要有三大功能:
1.一次顯示整個(gè)文件。$ cat filename
2.從鍵盤(pán)創(chuàng)建一個(gè)文件。$ cat > filename
只能創(chuàng)建新文件,不能編輯已有文件.
3.將幾個(gè)文件合并為一個(gè)文件: $cat file1 file2 > file
參數(shù):
-n 或 --number 由 1 開(kāi)始對(duì)所有輸出的行數(shù)編號(hào)
-b 或 --number-nonblank 和 -n 相似,只不過(guò)對(duì)于空白行不編號(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比較簡(jiǎn)單,就是cat 后面直接接文件名。
比如:
de>[root@localhost ~]# cat /etc/fstabde>
為了便于新手弟兄靈活掌握這個(gè)工具,我們多說(shuō)一點(diǎn)常用的參數(shù);
1.0 cat 語(yǔ)法結(jié)構(gòu);
de>cat [選項(xiàng)] [文件]…de>
選項(xiàng)
-A, --show-all 等價(jià)于 -vET
-b, --number-nonblank 對(duì)非空輸出行編號(hào)
-e 等價(jià)于 -vE
-E, --show-ends 在每行結(jié)束處顯示 $
-n, --number 對(duì)輸出的所有行編號(hào)
-s, --squeeze-blank 不輸出多行空行
-t 與 -vT 等價(jià)
-T, --show-tabs 將跳 字符顯示為 ^I
-u (被忽略)
-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外
–help 顯示此幫助信息并離開(kāi)
1.1 cat 查看文件內(nèi)容實(shí)例;
de>[root@localhost ~]# cat /etc/profile 注:查看/etc/目錄下的profile文件內(nèi)容;
[root@localhost ~]# cat -b /etc/fstab 注:查看/etc/目錄下的profile內(nèi)容,并且對(duì)非空白行進(jìn)行編號(hào),行號(hào)從1開(kāi)始;
[root@localhost ~]# cat -n /etc/profile 注:對(duì)/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 對(duì)于內(nèi)容極大的文件來(lái)說(shuō),可以通過(guò)管道|傳送到more 工具,然后一頁(yè)一頁(yè)的查看;
de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>
1.2 cat 的創(chuàng)建、連接文件功能實(shí)例;
cat 有創(chuàng)建文件的功能,創(chuàng)建文件后,要以EOF或STOP結(jié)束;
de>[root@localhost ~]# cat > linuxsir.org.txt << EOF 注:創(chuàng)建linuxsir.org.txt文件;
我來(lái)測(cè)試 cat 創(chuàng)建文件,并且為文件輸入內(nèi)容; 注:這是為linuxsir.org.txt文件輸入內(nèi)容;
北南南北 測(cè)試; 注:這是為linuxsir.org.txt文件輸入內(nèi)容;
EOF 注:退出編輯狀態(tài);
[root@localhost ~]# cat linuxsir.org.txt 注:我們查看一下linuxsir.org.txt文件的內(nèi)容;
我來(lái)測(cè)試 cat 創(chuàng)建文件,并且為文件輸入內(nèi)容;
北南南北 測(cè)試;de>
cat 還有向已存在的文件追加內(nèi)容的功能;
de>[root@localhost ~]# cat linuxsir.txt 注:查看已存在的文件linuxsir.txt 內(nèi)容;
I am BeiNanNanBei From LinuxSir.Org . 注:內(nèi)容行
我正在為cat命令寫(xiě)文檔
[root@localhost ~]# cat >> linuxsir.txt << EOF 注:我們向linuxsir.txt文件追加內(nèi)容;
我來(lái)測(cè)試cat向文檔追加內(nèi)容的功能; 注:這是追回的內(nèi)容
OK?
OK~
北南 呈上
EOF 注:以EOF退出;
[root@localhost ~]# cat linuxsir.txt 注:查看文件內(nèi)容,看是否追回成功。
I am BeiNanNanBei From LinuxSir.Org .
我正在為cat命令寫(xiě)文檔
我來(lái)測(cè)試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>
我想通過(guò)cat 把sir01.txt、sir02.txt及sir03.txt 三個(gè)文件連接在一起(也就是說(shuō)把這三個(gè)文件的內(nèi)容都接在一起)并輸出到一個(gè)新的文件sir04.txt 中。
注意:其原理是把三個(gè)文件的內(nèi)容連接起來(lái),然后創(chuàng)建sir04.txt文件,并且把幾個(gè)文件的內(nèi)容同時(shí)寫(xiě)入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.txt
linuxsir.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)建,>>是追加。千萬(wàn)不要弄混了。造成失誤可不是鬧著玩的;
本文是轉(zhuǎn)載的,不重復(fù)造輪子了。
總結(jié)
以上是生活随笔為你收集整理的Linux中的cat命令用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 实时竞价RTB广告平台_传漾科技_中国领
- 下一篇: linux mint19 无线网卡wif