上一篇咱們講到了cat的使用示例:http://www.javashuo.com/article/p-fdvkrsvv-bb.htmlhtml
本篇咱們將繼續延續Cat講講Taclinux
本文是咱們講講Linux技巧和竅門系列的一部分,在本文中,咱們將介紹cat命令和tac的一些基本用法。web
Cat指令示例session
Cat命令(Concatenate的縮寫)是* nix系統中最經常使用的命令之一。該命令最基本的用法是讀取文件並將其顯示到stdout,標示在終端上顯示文件的內容。ui
# cat file.txtthis
Calinux@Calinux ~ $ cat filel.txtspa
Calinux is best site for Linuxunix
Calinux@Calinux ~ $調試
cat命令的另外一種用法是將多個文件讀取或組合在一塊兒,如下示例所示。日誌
#cat file1.txt file2.txt file3.txt> file-all.txt Calinux@Calinux~ $ cat filel.txt file2.txt file3.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the pLanet.. Calinux@Calinux~$
該命令還可用於使用「>」Linux重定向運算符將多個文件鏈接(合併)爲一個文件。
#cat file1.txt file2.txt file3.txt> file-all.txt Calinux@Calinux ~ $ cat filel.txt file2.txt file3.txt > file-all.txt Calinux@Calinux ~ $ cat file-all.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the planet.. Calinux@Calinux ~ $
經過使用重定向程序,能夠file-all.txt使用如下語法將新文件的內容添加到的底部。
#cat file4.txt >> file-all.txt Calinux@Calinux~$ cat file4.txt Calinux is best site for Linux in the Universe.... Calinux@Calinux ~$ cat file4.txt >> file-all.txt Calinux@Calinux~$ cat file-all.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the planet.. Calinux is best site for Linux in the Universe... Calinux@Calinux~$
該命令可用於文件的內容複製到一個新的文件。新文件能夠重命名爲任意文件。例如,將文件從當前位置複製到/tmp/目錄。
#cat file1.txt> /tmp/file1.txt Calinux@Calinux ~ $ cat filel.txt>/tmp/filel. txt Calinux@Calinux~$ Is -l /tmp/filel.txt -rw-r--r--l Calinux Calinux 31 Apr 6 14:12 /tmp/filel.txt Calinux@Calinux~$
將文件從當前位置複製到/tmp/目錄並更改其名稱。
#cat file1.txt> /tmp/newfile.cfg Calinux@Calinux ~$ cat filel1.txt>/tmp/newfile.cfg Calinux@Calinux ~$ Is -1 /tmp/newfile.cfg -rw-r--r--l Calinux Calinux 31 Apr 6 15:19 /tmp/newfilt.cfg Calinux@Calinux ~ $
cat命令有一種較少用法是使用如下語法建立一個新文件。編輯完成後,點擊CTRL+D保存並退出新文件。
#cat> new_file.txt Calinux@Calinux ~ $ cat > new_file. txt this is my new file Calinux@Calinux ~ $ cat new_file. txt this is my new file Calinux@Calinux ~ $
爲了給文件的全部輸出行編號,包括空行,使用-n。
#cat -n file-all.txt |CalinuxeCalinux ~ $ cat -n file-all.txt 1 Calinux is best site for Linux 2 3 Calinux is best site for Linux on the web. 4 5 Calinux is best site for Linux on the planet.. 6 7 Calinux is best site for Linux in the Universe.... Calinux@Calinux ~$
要僅顯示每一個非空行的編號,使用-b。
Calinux@Calinux~ $ cat -b file-all.txt 1 Calinux is best site for Linux 2 Calinux is best site for Linux on the web. 3 Calinux is best site for Linux on the planet.. 4 Calinux is best site for Linux in the Universe.... Calinux@Calinux~ $
另外一方面,* nix系統中有一個不爲人知且使用較少的命令是Tac。Tac其實是cat命令的反向版本,該命令打印文件的每一行,從下一行開始,在上一行結束,直到計算機標準輸出。
#tac file-all.txt Calinux@tecamint ~ $ tac file-all.txt This is number four This is number three This is number two This is number one Calinux@tecamint ~ $
該命令最重要的選項之一是-s開關,它根據文件中的字符串或關鍵字將文件的內容分隔開。
Calinux@Calinux~$ tac file-all. txt --separator」two」 This is number three This is number four This is number one
接下來,tac命令最重要的用法是,它能夠爲調試日誌文件提供很大的幫助,從而顛倒日誌內容的時間順序。
$ tac /var/log/auth.log Or to display the last lines $ tail /var/log/auth.log | tac
Calinux@Calinux ~ $ tac /var/log/auth.log pr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session closed for user root ....
Calinux@Calinux ~ $ tail /var/log/auth.log | tac Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session closed for user root Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 15:55:02 Calinux CRON[17194]: pam_unix(cron:session): session closed for user root Apr 6 15:55:01 Calinux CRON[17195]: pam_unix(cron:session): session closed for user root ...
與cat命令相同,tac在處理文本方面作得很出色,可是在其餘類型的文件中尤爲應避免使用它,尤爲是二進制文件或第一行表示將運行該文件的程序的文件。