在 Linux 中,cat 命令是最常用的命令之一。 cat
代表“連接”,可以讀取、寫入和連接文件內容到標準輸出。 cat 命令通常用於查看一個或多個文本文件的內容,通過將一個文件的內容添加到另一個文件來合併文件並創建新文件。
在本教程中,我們將通過示例學習如何充分發揮 cat 命令的潛力。
一般語法
句法:
$ cat [options] [filename/filenames]
這 [options]
允許您對命令使用不同的參數。
[filename/filenames]
您可以在此處指定要顯示的一個或多個文件的名稱。
創建一個新文件
使用 cat 命令,您可以創建一個新文件並添加內容。
句法:
$ cat > [filename]
當您以這種方式創建文件時,光標將放置在一個新行上,您可以在其中鍵入您的內容。 寫完想要的內容後,就可以使用 Ctrl+D 完成編輯並保存。
例如:
$ cat > Test1.txt
輸出:
kali@kali:~/Desktop$ cat > Test1.txt
This is the first test file.
顯示文件內容
您可以使用 cat 命令通過簡單地輸入 cat 後跟文件名來顯示文件的內容。
句法:
$ cat [filename]
您可以使用 more
或者 less
如果要逐頁查看文件,請使用命令。
句法:
$ cat [filename] | more
$ cat [filename] | less
例如:
$ cat Test1.txt
輸出:
kali@kali:~/Desktop$ cat Test1.txt
This is the first test file.
kali@kali:~/Desktop$
顯示多個文件的內容
cat 命令可用於一次顯示多個文件的內容。 您可以使用 cat 命令,後跟用空格分隔的文件名列表。
句法:
$ cat [filename1] [filename2] ....
例子:
$ cat Test1.txt Test2.txt Test3.txt
輸出:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt
This is the first test file.
This is second test file.
This is the third test file.
kali@kali:~/Desktop$
將內容從一個文件複製到另一個文件
在 cat 命令中使用 (>) 運算符,我們可以將內容從一個文件複製到另一個文件。
句法:
$ cat [filename1] > [filename2]
如果 [filename2]
不存在,那麼 cat 命令會自動創建一個新的並複制 [filename1]
到 [filename2]
.
例如:
$ cat Test1.txt > Test2.txt
輸出:
kali@kali:~/Desktop$ cat Test1.txt > Test2.txt
kali@kali:~/Desktop$ cat Test2.txt
This is the first test file.
kali@kali:~/Desktop$
該命令將從中復制內容 Test1.txt
並將其覆蓋在 Test2.txt
. 您還可以使用 (>>) 運算符將源文本文件附加到目標文本文件,而不是覆蓋它。
句法:
$ cat [filename1] >> [filename2]
例如:
$ cat Test1.txt >> Test2.txt
輸出:
kali@kali:~/Desktop$ cat Test2.txt
This is the second test file.
kali@kali:~/Desktop$ cat Test1.txt
This is the first test file.
kali@kali:~/Desktop$ cat Test1.txt >> Test2.txt
kali@kali:~/Desktop$ cat Test2.txt
This is the second test file.
This is the first test file.
kali@kali:~/Desktop$
顯示文件中的行號
可以使用 -b 標誌以及 cat 命令和文件名來顯示所有非空文件。
句法:
$ cat -b [filename]
為了 example:
$ cat -b Test1.txt
輸出:
kali@kali:~/Desktop$ cat -b Test1.txt
1 This is the first test file.
2 This 3 is 4 test 5 file.
kali@kali:~/Desktop$
如果您還想顯示沒有字符的行,可以將 -n 標誌與 cat 命令和文件名一起使用。
句法:
$ cat -n [filename]
例如:
$ cat -n Test1.txt
輸出:
kali@kali:~/Desktop$ cat -n Test1.txt
1 This is the first test file.
2
3 This
4 is
5 test
6
7
8 file.
9
kali@kali:~/Desktop$
連接一個文件或多個文件
如果您想一次查看多個文件的內容,請使用 cat 命令將它們連接起來。
句法:
$ cat [filename1] [filename2]...
例如:
$ cat Test1.txt Test2.txt Test3.txt
輸出:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
您還可以通過使用 (>) 運算符創建新文件或更新現有文件,將多個文件合併為一個文件。
句法:
$ cat [filename1] [filename2]... > [filename3]
例如:
$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
自從 Test4.txt
不存在,它會創建一個名為的新文件 Test4.txt
並連接的內容 Test1.txt
, Test2.txt
, 和 Test3.txt
在 Test4.txt
.
輸出:
kali@kali:~/Desktop$ cat Test1.txt Test2.txt Test3.txt > Test4.txt
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
顯示文件中每一行的結尾
您可以使用 cat 命令確定文件中一行的結尾。 有時在行尾會出現空格等隱藏字符,可能會帶來錯誤或發現問題。 您可以使用帶有 -E 標誌的 cat 命令來將美元 ($) 顯示為行尾字符。
句法:
$ cat -E [filename]
例如:
$ cat -E Test4.txt
輸出:
kali@kali:~/Desktop$ cat -E Test4.txt
This is the first test file.$
$
$
This is the second test file.$
$
$
This is the first test file.$
$
$
This is the third test file.$
kali@kali:~/Desktop$
減少空行
當您顯示文件中的內容時,看到大量空行可能會令人不安。 cat 命令和 -s 可用於從輸出中刪除重複的空行。 cat 命令中的 -s 選項僅顯示一個空行並壓縮重複的行。
句法:
$ cat -s [filename]
例如:
$ cat -s Test4.txt
輸出:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ cat -s Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$
第一個輸出沒有使用 -s 選項,第二個輸出是在使用 -s 選項之後。
顯示標籤
-T 選項與 cat 命令一起顯示文件內容以及文本中的製表符空間。
製表符空間由符號 ^I 表示。
句法:
$ cat -T [filename]
例如:
$ cat -T Test4.txt
輸出:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ cat -T Test4.txt
^IThis is the first test file.
This is the second test file.
^IThis is the first test file.
This is the ^Ithird test file.
kali@kali:~/Desktop$
第一個輸出沒有使用 -T 選項,第二個輸出是在使用 -T 選項之後。
以相反的順序顯示文件的內容
tac 命令與 cat 命令相反。 tac 將按照與文本文件內容相反的順序顯示輸出。
句法:
$ tac [filename]
例如:
$ tac Test4.txt
輸出:
kali@kali:~/Desktop$ cat Test4.txt
This is the first test file.
This is the second test file.
This is the first test file.
This is the third test file.
kali@kali:~/Desktop$ tac Test4.txt
This is the third test file.
This is the first test file.
This is the second test file.
This is the first test file.
第一個輸出是使用 cat 命令獲得的,第二個輸出是使用 tac 命令獲得的。
如果您想了解有關 cat 命令的更多信息或有任何困惑,請使用 help 命令。
$ cat --help
輸出:
kali@kali:~$ cat --help
Usage: cat [OPTION]… [FILE]…
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.
kali@kali:~$
結論
在本教程中,我們了解了 cat 命令、它與各種選項的用法和示例。 Cat 是一個有用的命令,可讓您創建和查看多種文本文件。 可以使用 cat 命令以多種方式同時顯示多個文件。