Linux運維基礎之cat命令詳解

Linux運維基礎之cat命令詳解

cat 命令是 “連線”(concatenate)的縮寫,是 Linux 中常用的工具,可讓使用者檢視、建立和集中檔案,或重定向其輸出。在本篇文章中,我們將探討新老 Linux 使用者如何利用 cat 命令完成各種任務。

cat 命令的一般語法如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ cat [OPTION] [FILE]...
$ cat [OPTION] [FILE]...
$ cat [OPTION] [FILE]...

1. 使用 cat 建立新檔案並新增內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat > filename
cat > filename
cat > filename

例子:

命令 cat > vegetables.txt 接收標準輸入,並將其重定向到名為 “vegetables.txt” 的檔案。執行該命令時,終端不會顯示任何輸出,而是等待你從鍵盤輸入文字。

你輸入的任何內容都將被寫入 “vegetables.txt”。要完成並儲存檔案,你需要按 CTRL-D(或在使用某些終端應用程式的 Windows 系統上按 CTRL-Z)。

下面是一個如何使用它的示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ cat > vegetables.txt
Carrot
Broccoli
Spinach
^D
$ cat > vegetables.txt Carrot Broccoli Spinach ^D
$ cat > vegetables.txt
Carrot
Broccoli
Spinach
^D

此時,將建立一個名為 “vegetables.txt” 的檔案,內容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Carrot
Broccoli
Spinach
Carrot Broccoli Spinach
Carrot
Broccoli
Spinach

2. 用 cat 顯示檔案內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat filename
cat filename
cat filename

例子:

假設該檔案包含一個常見蔬菜列表;執行 cat vegetables.txt 命令將在終端中顯示該檔案的內容。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Carrots
Broccoli
Spinach
Tomatoes
Cucumbers
Peppers
Onions
Potatoes
Kale
Lettuce
Carrots Broccoli Spinach Tomatoes Cucumbers Peppers Onions Potatoes Kale Lettuce
Carrots
Broccoli
Spinach
Tomatoes
Cucumbers
Peppers
Onions
Potatoes
Kale
Lettuce

3. 用 cat 顯示多個檔案的內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat filename_1 filename_2
cat filename_1 filename_2
cat filename_1 filename_2

例子:

假設檔案 fruits.txt 和 vegetables.txt 的內容分別如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Apple Banana Cherry
Apple
Banana
Cherry
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Carrot
Lettuce
Tomato
Carrot Lettuce Tomato
Carrot
Lettuce
Tomato

命令 cat fruits.txt vegetables.txt 將產生結果:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Carrot
Lettuce
Tomato
Apple Banana Cherry Carrot Lettuce Tomato
Apple
Banana
Cherry
Carrot
Lettuce
Tomato

4. 用 cat 行號顯示內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat -n filename
cat -n filename
cat -n filename

例子:

如果 fruits.txt 的內容與上例相同:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Apple Banana Cherry
Apple
Banana
Cherry

然後,命令 cat -n fruits.txt 就會生成:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 Apple
2 Banana
3 Cherry
1 Apple 2 Banana 3 Cherry
1	Apple
2	Banana
3	Cherry

5. 使用 cat 複製、替換或複製檔案內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat filename new_filename
cat filename new_filename
cat filename new_filename

例子:

命令 cat fruits.txt > new_fruits.txt 將把 fruits.txt 中的內容寫入一個名為 new_fruits.txt 的新檔案。假設命令執行成功,終端不會顯示任何輸出。

如果 fruits.txt 的內容與之前的相同:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Apple Banana Cherry
Apple
Banana
Cherry

執行該命令後, new_fruits.txt 的內容將是

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Apple Banana Cherry
Apple
Banana
Cherry

6. 使用 cat 將多個檔案的內容合併為一個檔案

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat filename_1 filename_2 > filename_3
cat filename_1 filename_2 > filename_3
cat filename_1 filename_2 > filename_3

例子:

命令 cat fruits.txt vegetable.txt > grocery.txt 將連線 fruits.txtvegetable.txt 的內容,然後將輸出重定向到名為 grocery.txt 的檔案。如果其中一個輸入檔案不存在,終端將顯示一條錯誤資訊,但你不會在終端中看到連線後的內容,因為它們被重定向到了 grocery.txt

假設 fruits.txt 包含:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Apple Banana Cherry
Apple
Banana
Cherry

vegetable.txt 包含:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Carrot
Lettuce
Tomato
Carrot Lettuce Tomato
Carrot
Lettuce
Tomato

執行該命令後,終端不會顯示任何輸出(除非出現錯誤),但檔案 grocery.txt 將包含以下內容

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Apple
Banana
Cherry
Carrot
Lettuce
Tomato
Apple Banana Cherry Carrot Lettuce Tomato
Apple
Banana
Cherry
Carrot
Lettuce
Tomato

如果 vegetable.txt 拼寫錯誤或不存在,你會在終端中看到類似的錯誤:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat: vegetable.txt: No such file or directory
cat: vegetable.txt: No such file or directory
cat: vegetable.txt: No such file or directory

更多 Linux 命令

下面羅列了最常見的一些 Linux 命令,您可以根據自己的需要查閱對應命令的詳細解析:

目錄操作 rmdir · cd · pwd · exa · ls
檔案操作 cat · cp · dd · less · touch · ln · rename · more · head
檔案系統操作 chown · mkfs · locate
網路 ping · curl · wget · iptables · mtr
搜尋和文字處理 find · grep · sed · whatis · ripgrep · fd · tldr
系統資訊和管理 env · history · top · who · htop · glances · lsof
使用者和會話管理 screen · su · sudo · open

此外,我們還整理 Linux 命令列大全,以幫助大家全面深入地學習 Linux。

評論留言