📜  Linux 中的 Cat 命令及示例

📅  最后修改于: 2022-05-13 01:57:32.287000             🧑  作者: Mango

Linux 中的 Cat 命令及示例

Cat(concatenate) 命令在 Linux 中非常常用。它从文件中读取数据并将其内容作为输出。它帮助我们创建、查看、连接文件。那么让我们来看看一些常用的 cat 命令。

1) 查看单个文件
命令:

$cat filename

输出

It will show content of given filename

2)查看多个文件
命令:

$cat file1 file2

输出



This will show the content of file1 and file2.

3) 查看以行号开头的文件的内容。
命令:

$cat -n filename

输出

It will show content with line number
example:-cat-n  geeks.txt
1)This is geeks
2)A unique array

4)创建文件
命令:

$ cat >newfile

输出

Will create and a file named newfile

5) 将一个文件的内容复制到另一个文件中。
命令:

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

输出

The content will be copied in destination file

6) cat 命令可以抑制输出中重复的空行
命令:

$cat -s geeks.txt

输出



Will suppress repeated empty lines in output

7) Cat 命令可以将一个文件的内容附加到另一个文件的末尾。
命令:

$cat file1 >> file2

输出

Will append the contents of one file to the end of another file

8) cat 命令可以使用 tac 命令以相反的顺序显示内容。
命令:

$tac filename

输出

Will display content in reverse order 

9) cat 命令可以高亮行尾。
命令:

$cat -E "filename"

输出

Will highlight the end of line

10) 如果你想同时使用 -v、-E 和 -T 选项,那么你可以只使用 -A 命令行选项而不是在命令中写入 -vET。
命令

$cat -A  "filename"

11) Cat 命令打开虚线文件。
命令:

$cat -- "-dashfile"

输出

Will display the content of -dashfile

12) 如果文件内容很多,终端放不下,则使用 cat 命令。
命令:



$cat "filename" | more

输出

Will show that much content, which could fit in terminal and will ask to show more.

12) Cat 命令合并多个文件的内容。
命令:

$cat "filename1" "filename2" "filename3" > "merged_filename"

输出

Will merge the contents of file in respective order and will insert that content in "merged_filename".

13) cat 命令显示文件夹中所有文本文件的内容。
命令:

$cat *.txt

输出

Will show the content of all text files present in the folder.

14) Cat 命令写入一个已经存在的文件。

命令 :

$cat >> geeks.txt
The newly added text.

输出

Will append the text "The newly added text." to the end of the file.