標籤:
在Linux系統中,大多數設定檔、記錄檔,甚至shell指令碼都使用文字檔格式,因此,Linux系統存在著多種文字編輯器,但當你僅僅想要查看一下這些檔案的內容時,可使用一個簡單的命令-cat。
cat手冊裡這樣描述:
cat命令讀取檔案內容,並輸出到標準裝置上面。
cat是一條linux內建命令. 幾乎所有linux發行版都內建(譯註:或者說我從未聽說過不內建cat命令的發行版)。接下來,讓我們開始學習如何使用。
1. 顯示檔案內容
最簡單的方法是直接輸入‘cat file_name’.
# cat /etc/issueCentOS release 5.10 (Final)Kernel \r on an \m
2. 同時顯示行號
當在讀取內容很多的設定檔時,如果同時顯示行號將會使操作變簡單,加上-n參數可以實現.
# cat -n /etc/ntp.conf1 # Permit time synchronization our time resource but do not2 # permit the source to query or modify the service on this system3 restrict default kod nomodify notrap nopeer noquery4 restrict -6 default kod nomodify notrap nopeer noquery56 # Permit all access over the loopback interface. This could be7 # tightened as well, but to do so would effect some of the8 # administration functions9 restrict 127.0.0.110 restrict -6 ::1
3. 在非空格行首顯示行號
類似於-n參數,-b也可以顯示行號。區別在於-b只在非空行前顯示行號。
#cat -b /etc/ntp.conf1 # Permit time synchronization our time resource but do not2 # permit the source to query or modify the service on this system3 restrict default kod nomodify notrap nopeer noquery4 restrict -6 default kod nomodify notrap nopeer noquery5 # Permit all access over the loopback interface. This could be6 # tightened as well, but to do so would effect some of the7 # administration functions8 restrict 127.0.0.19 restrict -6 ::1
4. 顯示tab定位字元
當你想要顯示文本中的tab製表位時. 可使用-T參數. 它會在輸入結果中標識為 ^I .
# cat -T /etc/hosts# Do not remove the following line, or various programs # that require network functionality will fail.127.0.0.1^I^Ilocalhost.localdomain localhost::1^I^Ilocalhost6.localdomain6 localhost6
5. 顯示分行符號
-E參數在每行結尾使用 $ 表示分行符號。如下所示 :
# cat -E /etc/hosts# Do not remove the following line, or various programs$# that require network functionality will fail.$127.0.0.1 localhost.localdomain localhost$::1 localhost6.localdomain6 localhost6$
6. 同時顯示定位字元及分行符號
當你想要同時達到-T及-E的效果, 可使用-A參數.
# cat -A /etc/hosts# Do not remove the following line, or various programs$# that require network functionality will fail.$127.0.0.1^I^Ilocalhost.localdomain localhost$::1^I^Ilocalhost6.localdomain6 localhost6$
7. 分屏顯示
當檔案內容顯示超過了你的螢幕大小, 可結合cat命令與其它命令分屏顯示。使用管道符 ( | )來串連。
# cat /proc/meminfo | less# cat /proc/meminfo | more
在less與more顯示結果的區別在於less參數可pageup及pagedown上下翻滾。而more僅能使用空格向下翻屏。
8. 同時查看2個檔案中的內容
位於/root檔案夾裡有兩個檔案取名linux及desktop,每個檔案含有以下內容 :
Linux : ubuntu, centos, redhat, mint and slackware
Desktop : gnome kde, xfce, enlightment, and cinnamon
當你想同時查看兩檔案中的內容時,可按如下方法 :
# cat /root/linux /root/desktopubuntucentosredhatmintslackwaregnomekdexfceenlightmentcinnamon
9. 排序顯示
類似. 你也可以結合cat命令與其它命令來進行自訂輸出. 如結合 sort ,通過管道符對內容進行排序顯示。舉例 :
# cat /root/linux | sortcentosmintredhatslackwareUbuntu
10. 輸入重新導向
你也可將顯示結果輸出重新導向到螢幕或另一個檔案。 只需要使用 > 符號(大於符號)即可輸出產生到另一個檔案。
# cat /root/linux > /root/linuxdistro
以上命令會產生一個與/root/linux內容一模一樣的叫linuxdistro的檔案.
11. 建立檔案
Linux下有多種方法建立檔案。其中使用cat就是方法之一.
# cat > operating_systemUnixLinuxWindowsMacOS
當你輸入cat > operatingsystem,它會產生一個operatingsystem的檔案。然後下面會顯示空行。此時你可輸入內容。比如我們輸入Unix, Linux, Windows 和 MacOS, 輸入完成後,按Ctrl-D存檔退出cat。此時你會發現當前檔案夾下會產生一個包含你剛才輸入內容的叫operating_system的檔案。
12.向檔案中追加內容
當你使用兩個 > 符時, 會將第一個檔案中的內容追加到第二個檔案的末尾。 舉例 :
# cat /root/linux >> /root/desktop# cat /root/desktop
它會將 /root/linux的內容追加到/root/desktop檔案的末尾。
第二個檔案的內容將會變成這樣:
gnomekdexfceenlightmentcinnamonubuntucentosredhatmintslackware
13. 重新導向輸入
你可使用 <命令(小於符號)將檔案輸入到cat中.
# cat < /root/linux
上面命令表示 /root/linux中的內容作為cat的輸入。螢幕上顯示如下 :
ubuntucentosredhatmintslackware
為了更清楚表示它的意義,我們使用以下命令 :
# cat < /root/linux | sort > linux-sort
此命令這樣理解: 從/root/linux中讀取內容,然後排序,將結果輸出並產生linux-sort新檔案
然後我們看看linux-sort中的內容 :
centosmintredhatslackwareubuntu
以上是一些cat命令的日常基本應用. 更多相關你可從cat命令手冊中學到並記得經常練習它們.
13個Cat命令管理檔案執行個體匯總