在Linux系統中,大多數配置文件、日志文件,甚至shell腳本都使用文本文件格式,因此,Linux系統存在著多種文本編輯器,但當你僅僅想要查看一下這些文件的內容時,可使用一個簡單的命令-cat。
cat手冊裡這樣描述:
cat命令讀取文件內容,並輸出到標准設備上面
cat是一條linux內置命令. 幾乎所有linux發行版都內置(譯注:或者說我從未聽說過不內置cat命令的發行版)。接下來,讓我們開始學習如何使用.
最簡單的方法是直接輸入‘cat file_name’.
# cat /etc/issue CentOS release 5.10 (Final) Kernel \r on an \m
當在讀取內容很多的配置文件時,如果同時顯示行號將會使操作變簡單,加上-n參數可以實現.
# cat -n /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 6 # Permit all access over the loopback interface. This could be 7 # tightened as well, but to do so would effect some of the 8 # administration functions 9 restrict 127.0.0.1 10 restrict -6 ::1
類似於-n參數,-b也可以顯示行號。區別在於-b只在非空行前顯示行號。
#cat -b /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 # Permit all access over the loopback interface. This could be 6 # tightened as well, but to do so would effect some of the 7 # administration functions 8 restrict 127.0.0.1 9 restrict -6 ::1
當你想要顯示文本中的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
-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$
當你想要同時達到-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$
當文件內容顯示超過了你的屏幕大小, 可結合cat命令與其它命令分屏顯示。使用管道符 ( | )來連接。
# cat /proc/meminfo | less # cat /proc/meminfo | more
在less與more顯示結果的區別在於less參數可pageup及pagedown上下翻滾。而more僅能使用空格向下翻屏。
位於/root文件夾裡有兩個文件取名linux及desktop,每個文件含有以下內容 :
Linux : Ubuntu, centos, RedHat, mint and slackware
Desktop : gnome kde, xfce, enlightment, and cinnamon
當你想同時查看兩文件中的內容時,可按如下方法 :
# cat /root/linux /root/desktop ubuntu centos redhat mint slackware gnome kde xfce enlightment cinnamon
更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2013-12/93540p2.htm