1. 程式人生 > >【Linux檔案管理命令cat用法】

【Linux檔案管理命令cat用法】

[cat]

1:檢視檔案內容 (cat + 檔名)

[email protected]:~#cat s.txt

~~~~~

~~~~~

[email protected]:~#

2:建立檔案(cat + >檔名  + <<結束符標誌)

[email protected]:~#cat >s.txt << OFF

>>OFF

[email protected]:~#

若沒有此檔案,將在當前目錄下建立s.txt,若有,則替換s.txt檔案,內容清空。

3:查詢檔案中的內容(cat + 檔名  +  | grep + 查詢內容)

[email protected]:~#cat  s.txt  | grep hello

printf("hello world");

[email protected]:~#

4:向檔案中寫入內容(cat + >檔名 + <<結束符 或者 cat + >>檔名 + <<結束符)

[email protected]:~#cat >s.txt <<OFF

>>hello world

>>thanks

>>OFF

[email protected]:~#cat  s.txt

hello world

thanks

[email protected]:~#cat >>s.txt <<OFF

>>i am a hero

>>OFF

[email protected]:~#cat s.txt

hello world

thanks

i am a hero

[email protected]:~#

5:把一個檔案的內容寫入到另一個檔案(cat + 原始檔名 + > +  要寫入的檔名)

[email protected]:~#cat  s.txt  > t.txt

[email protected]:~#cat t.txt

hello world

thanks

i am a hero

[email protected]:~#

6:把兩個檔案的內容合併到另一個檔案(cat + 原始檔1名 + 原始檔2名 + > > +  要寫入的檔名)

[email protected]:~#cat  s.txt   t.txt  >> h.txt

[email protected]:~#

備註:

語法格式

cat [-AbeEnstTuv] [--help] [--version] fileName

引數說明

 -n 或 --number 由 1 開始對所有輸出的行數編號

 -b 或 --number-nonblank 和 -n 相似,只不過對於空白行不編號

 -s 或 --squeeze-blank 當遇到有連續兩行以上的空白行,就代換為一行的空白行

 -v 或 --show-nonprinting