萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux 檔案內容檢視工具使用指南:cat、more、less、head、tail 完整教學

摘要:本文介紹 Linux 中幾種常用的檔案內容檢視工具,包括 cat、more、less、head、tail 等,涵蓋它們最常用的參數與操作方式,幫助新手在短時間內快速上手。本指南專為面對指令列控制台感到困惑的初學者而寫。

目錄

  1. cat — 顯示與連接檔案內容的工具
  2. more — 檔案內容分頁檢視工具
  3. less — 靈活的檔案內容檢視工具
  4. head — 顯示檔案開頭幾行
  5. tail — 顯示檔案末尾幾行
  6. 參考文檔

1、cat — 顯示與連接檔案內容的工具

cat 是一個文字檔案檢視與連接工具。檢視檔案內容非常簡單,直接在 cat 後面接上檔案名即可。

例如:

[root@localhost ~]# cat /etc/fstab

1.0 cat 語法結構

cat [選項] [檔案]...

常用選項:

  • -A, --show-all:等價於 -vET
  • -b, --number-nonblank:對非空輸出行編號
  • -E, --show-ends:在每行結尾處顯示 $
  • -n, --number:對輸出的所有行編號
  • -s, --squeeze-blank:不輸出多行空行
  • -T, --show-tabs:將跳格字元顯示為 ^I
  • -v, --show-nonprinting:使用 ^ 和 M- 引用(LFD 和 TAB 除外)
  • --help:顯示幫助資訊並離開

1.1 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

1.2 cat 的建立與連接檔案功能

建立檔案: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

2、more — 檔案內容分頁檢視工具

more 是最常用的工具之一,能將輸出內容根據視窗大小進行分頁顯示,並提示檔案的百分比。

[root@localhost ~]# more /etc/profile

2.1 more 的語法、參數與指令

more [參數選項] [檔案]

常用參數:

  • +num:從第 num 行開始顯示
  • -num:定義螢幕大小,為 num 行
  • +/pattern:從 pattern 前兩行開始顯示
  • -c:從頂部清屏後顯示
  • -d:提示「Press space to continue, 'q' to quit」,禁用響鈴
  • -l:忽略 Ctrl+L(換頁)字元
  • -p:通過清除視窗而非捲屏來換頁
  • -s:將連續多個空行合併為一行
  • -u:去掉檔案內容中的下劃線

退出 more 的動作指令是 q。

2.2 more 參數應用舉例

[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 單詞的前兩行開始顯示

2.3 more 的動作指令

檢視內容較大的檔案時,more 的動作指令非常實用:

  • Enter:向下捲動 n 行(需定義,預設為 1 行)
  • Ctrl+F:向下捲動一屏
  • 空格鍵:向下捲動一屏
  • Ctrl+B:返回上一屏
  • =:輸出當前行的行號
  • :f:輸出檔案名和當前行的行號
  • v:呼叫 vi 編輯器
  • ! 指令:呼叫 Shell 並執行指令
  • q:退出 more

檢視檔案時想呼叫 vi 來編輯,使用 v 動作指令即可,非常方便。

2.4 其他指令與 more 結合的運用

當列出目錄下檔案內容太多時,可以結合管道符 | 與 more 來分頁顯示。例如:

[root@localhost ~]# ls -l /etc | more

3、less — 靈活的檔案內容檢視工具

less 與 more 類似,但功能更為強大,支援向前和向後捲動檢視檔案內容。

3.1 less 的語法格式

less [選項] [檔案]

3.2 less 的動作指令

  • 空格鍵 / Page Down:向下翻一頁
  • b / Page Up:向上翻一頁
  • Enter:向下捲動一行
  • k:向上捲動一行
  • /pattern:向前搜尋 pattern
  • ?pattern:向後搜尋 pattern
  • n:重複上一次搜尋
  • N:反向重複上一次搜尋
  • q:退出 less

4、head — 顯示檔案開頭幾行

head 工具用於顯示檔案內容的前幾行,預設顯示前 10 行。

[root@localhost ~]# head /etc/profile 註:顯示 profile 檔案的前 10 行
[root@localhost ~]# head -n 5 /etc/profile 註:顯示前 5 行

5、tail — 顯示檔案末尾幾行

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 的使用者來說,這些工具是必備的基本技能。建議在實際操作中多加練習,逐漸熟悉各參數的用途。

copyright © 萬盛學電腦網 all rights reserved