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。

评论留言