linux下cat命令詳解

來源:互聯網
上載者:User

簡略版:

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  
     

詳細點的:

轉:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html

cat 是一個文字檔查看和串連工具。查看一個檔案的內容,用cat比較簡單,就是cat 後面直接接檔案名稱。

比如:

de>[root@localhost ~]# cat /etc/fstabde>

為了便於新手弟兄靈活掌握這個工具,我們多說一點常用的參數;



1.0 cat 文法結構;

de>cat [選項] [檔案]...de>
選項
-A, --show-all 等價於 -vET
-b, --number-nonblank 對非空輸出行編號
-e 等價於 -vE
-E, --show-ends 在每行結束處顯示 $
-n, --number 對輸出的所有行編號
-s, --squeeze-blank 不輸出多行空行
-t 與 -vT 等價
-T, --show-tabs 將跳 字元顯示為 ^I
-u (被忽略)
-v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外
--help 顯示此協助資訊並離開



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命令寫文檔


我來測試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

123456

i am testing

56789

BeiNan Tested

09876

linuxsir.org testingde>

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

linuxsir.org forever

123456

i am testing

56789

BeiNan Tested

09876

linuxsir.org testingde>

警告:我們要知道>意思是建立,>>是追加。千萬不要弄混了。造成失誤可不是鬧著玩的;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.