摘要:本文介紹 Linux 中幾種常用的檔案內容檢視工具,包括 cat、more、less、head、tail 等,涵蓋它們最常用的參數與操作方式,幫助新手在短時間內快速上手。本指南專為面對指令列控制台感到困惑的初學者而寫。
cat 是一個文字檔案檢視與連接工具。檢視檔案內容非常簡單,直接在 cat 後面接上檔案名即可。
例如:
[root@localhost ~]# cat /etc/fstab
cat [選項] [檔案]...
常用選項:
[root@localhost ~]# cat /etc/profile 註:檢視 /etc/ 目錄下的 profile 檔案內容
[root@localhost ~]# cat -b /etc/fstab 註:對非空白行進行編號,行號從 1 開始
[root@localhost ~]# cat -n /etc/profile 註:對所有行(含空白行)進行編號輸出顯示
[root@localhost ~]# cat -E /etc/profile 註:在每行結尾處附加 $ 符號
cat 加參數 -n 與 nl 工具效果類似,都會在每行前面加上行號:
[root@localhost ~]# cat -n /etc/profile [root@localhost ~]# nl /etc/profile
cat 可以同時顯示多個檔案的內容:
[root@localhost ~]# cat /etc/fstab /etc/profile
對於內容極大的檔案,可以透過管道將輸出傳送給 more 工具進行分頁檢視:
[root@localhost ~]# cat /etc/fstab /etc/profile | more
建立檔案:cat 具有建立檔案的功能,建立後需以 EOF 或 STOP 結束輸入。
[root@localhost ~]# cat > linuxsir.org.txt << EOF 註:建立 linuxsir.org.txt 檔案 > 我來測試 cat 建立檔案,並為檔案輸入內容 > 北南南北 測試 > EOF 註:退出編輯狀態
[root@localhost ~]# cat linuxsir.org.txt 註:檢視檔案內容
追加內容:cat 還能向已存在的檔案追加內容。
[root@localhost ~]# cat linuxsir.txt 註:檢視已存在的檔案 linuxsir.txt 內容 I am BeiNanNanBei From LinuxSir.Org. 我正在為 cat 指令寫文檔
[root@localhost ~]# cat >> linuxsir.txt << EOF 註:向 linuxsir.txt 檔案追加內容 > 我來測試 cat 向文檔追加內容的功能 > OK? > OK~ > 北南 呈上 > EOF 註:以 EOF 退出
連接多個檔案並輸出到新檔案:
假設我們有 sir01.txt、sir02.txt 和 sir03.txt,內容如下:
[root@localhost ~]# cat sir01.txt 123456 i am testing [root@localhost ~]# cat sir02.txt 56789 BeiNan Tested [root@localhost ~]# cat sir03.txt 09876 linuxsir.org testing
將三個檔案連接在一起並輸出到 sir04.txt:
[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt [root@localhost ~]# more sir04.txt
注意:> 是建立檔案,>> 是追加內容。千萬不要弄混,否則可能造成資料遺失。
將多個檔案內容追加到已存在的檔案中:
[root@localhost ~]# cat sir00.txt linuxsir.org forever [root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt [root@localhost ~]# cat sir00.txt linuxsir.org forever 123456 i am testing 56789 BeiNan Tested 09876 linuxsir.org testing
more 是最常用的工具之一,能將輸出內容根據視窗大小進行分頁顯示,並提示檔案的百分比。
[root@localhost ~]# more /etc/profile
more [參數選項] [檔案]
常用參數:
退出 more 的動作指令是 q。
[root@localhost ~]# more -dc /etc/profile 註:顯示提示,並從終端頂部顯示
[root@localhost ~]# more +4 /etc/profile 註:從 profile 的第 4 行開始顯示
[root@localhost ~]# more -4 /etc/profile 註:每屏顯示 4 行
[root@localhost ~]# more +/MAIL /etc/profile 註:從 profile 中第一個 MAIL 單詞的前兩行開始顯示
檢視內容較大的檔案時,more 的動作指令非常實用:
檢視檔案時想呼叫 vi 來編輯,使用 v 動作指令即可,非常方便。
當列出目錄下檔案內容太多時,可以結合管道符 | 與 more 來分頁顯示。例如:
[root@localhost ~]# ls -l /etc | more
less 與 more 類似,但功能更為強大,支援向前和向後捲動檢視檔案內容。
less [選項] [檔案]
head 工具用於顯示檔案內容的前幾行,預設顯示前 10 行。
[root@localhost ~]# head /etc/profile 註:顯示 profile 檔案的前 10 行 [root@localhost ~]# head -n 5 /etc/profile 註:顯示前 5 行
tail 工具用於顯示檔案內容的最後幾行,預設顯示最後 10 行。常用於檢視日誌檔案的最新內容。
[root@localhost ~]# tail /var/log/messages 註:顯示 messages 日誌的最後 10 行 [root@localhost ~]# tail -n 20 /var/log/messages 註:顯示最後 20 行 [root@localhost ~]# tail -f /var/log/messages 註:持續監控日誌新增內容(常用於即時檢視日誌更新)
本文介紹了 Linux 中幾種常用的檔案內容檢視工具,涵蓋 cat、more、less、head 和 tail 的基本用法與常用參數。掌握這些工具能大幅提升在指令列環境下檢視和管理檔案的效率。
對於剛接觸 Linux 的使用者來說,這些工具是必備的基本技能。建議在實際操作中多加練習,逐漸熟悉各參數的用途。