- Linux管理員教程
- 首頁
- CentOS概述
- 基本的CentOS Linux命令
- 檔案/資料夾管理
- 使用者管理
- 配額管理
- Systemd服務啟動和停止
- 使用systemctl進行資源管理
- 使用cgroups進行資源管理
- 程序管理
- 防火牆設定
- 在CentOS Linux中配置PHP
- 在CentOS Linux中設定Python
- 在CentOS Linux中配置Ruby
- 為CentOS Linux設定Perl
- 安裝和配置Open LDAP
- 建立SSL證書
- 安裝Apache Web伺服器CentOS 7
- 在CentOS 7上設定MySQL
- 設定Postfix MTA和IMAP/POP3
- 安裝匿名FTP
- 遠端管理
- CentOS中的流量監控
- 日誌管理
- 備份和恢復
- 系統更新
- Shell指令碼
- 包管理
- 卷管理
- Linux管理員有用資源
- Linux管理員 - 快速指南
- Linux管理員 - 有用資源
- Linux管理員 - 討論
Linux管理員 - cat命令
cat命令用於連線檔案並列印到標準輸出。之前,我們已經演示了cat命令的使用和濫用。cat服務於以下不同的目的:
顯示檔案內容
將一個檔案的內容寫入另一個檔案
將多個檔案合併到一個檔案中
支援特殊功能:新增行號、顯示特殊字元、消除空行
| 開關 | 操作 |
|---|---|
| -b | 編號非空行 |
| -E | 顯示行尾 |
| -T | 顯示製表符 |
| -s | 壓縮空白,抑制重複的空行 |
如前所述,在使用grep、sort和uniq等實用程式時,我們希望儘可能避免將cat的輸出透過管道傳輸。我們之前在簡單演示管道命令時這樣做了。但是,知道何時使用grep之類的實用程式執行操作才能將Linux管理員與Linux終端使用者區分開來。
壞習慣
[root@centosLocal centos]# cat /etc/passwd | sort -t: -k1 | grep ":0" halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync [root@centosLocal centos]#
好習慣
[root@centosLocal centos]# grep ":0" /etc/passwd | sort -t: -k 1 halt:x:7:0:halt:/sbin:/sbin/halt operator:x:11:0:operator:/root:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync [root@centosLocal centos]#
注意 - 將cat透過管道傳輸到諸如sort或grep之類的輔助命令僅在需要時才應執行。
cat的一個常見用法是處理Windows格式的行分隔符。Linux和Windows都根據內部設計,使用不同的控制程式碼來表示行尾(EOL):
* Linux line break is always a Line Feed: LF or depicted as "\n". * Windows is Carriage Return followed by a Line Feed: CR LF or depicted as "\r\n". * Macintosh, in all moderne releases of OS X and now macOS, has adopted the Linux/Unix standard of LF or "\n"
因此,假設我們在像gedit這樣的GUI文字編輯器中開啟我們的檔案,或者在應用過濾命令時遇到隨機問題。文字顯示在單行上,或者過濾命令無法按預期執行。
特別是當文字檔案是從網際網路上下載時,我們希望檢查行分隔符。以下是cat顯示EOL字元的示例輸出。
[root@centosLocal centos]# cat -E ./Desktop/WinNames.txt $ed:Daniel:101 $enny:Colon:608 $ana:Maxwell:602 $arian:Little:903 $obbie:Chapman:403 $icolas:Singleton:203 $ale:Barton:901
注意每行前面的“$”?Linux正在讀取CR“\n”,從而中斷檔案。然後將回車符轉換到每個檔案的第一個字元上。
在沒有-E開關的情況下檢視時,檔案看起來不錯:
[root@centosLocal centos]# cat ./Desktop/WinNames.txt Ted:Daniel:101 Jenny:Colon:608 Dana:Maxwell:602 Marian:Little:903 Bobbie:Chapman:403 Nicolas:Singleton:203 Dale:Barton:901
幸運的是,使用Linux過濾命令,這是一個簡單的修復:
[root@centosLocal centos]# sed -i 's/\r$//g' ./Desktop/WinNames.txt [root@centosLocal centos]# cat -E ./Desktop/WinNames.txt Ted:Daniel:101$ Jenny:Colon:608$ Dana:Maxwell:602$
注意 - 使用-E開關檢視時,所有Linux換行符都將以$結尾。
cat還可以用於將多個檔案合併到一個檔案中。
[root@centosLocal centos]# cat linux.txt CentOS Ubuntu Red Hat Suse Debian [root@centosLocal centos]# cat windwos.txt NT 3.5 NT 4.0 Server 2000 Server 2003 Server 2008 Server 2012 Server 2016 [root@centosLocal centos]#
現在讓我們使用cat將這兩個檔案合併。
[root@centosLocal centos]# cat windwos.txt linux.txt > server_class_operating_sytems.txt [root@centosLocal centos]# cat server_class_operating_sytems.txt NT 3.5 NT 4.0 Server 2000 Server 2003 Server 2008 Server 2012 Server 2016 CentOS Ubuntu Red Hat Suse Debian [root@centosLocal centos]#
最後,我們可以使用-n開關對每一行輸出進行編號。這將給我們一個總行數。
[root@centosLocal centos]# cat -n ./server_class_operating_sytems.txt 1 NT 3.5 2 NT 4.0 3 Server 2000 4 Server 2003 5 Server 2008 6 Server 2012 7 Server 2016 8 CentOS 9 Ubuntu 10 Red Hat 11 Suse 12 Debian [root@centosLocal centos]#
basic_centos_linux_commands.htm
廣告