cat Commandcat命令

Concatenate and display file contents连接并显示文件内容

What is cat?什么是cat?

The cat command (short for "concatenate") is one of the most frequently used commands in Unix/Linux. It reads files sequentially, writing them to standard output. You can use it to view file contents, combine files, and create new files.

cat命令("concatenate"的缩写)是Unix/Linux中最常用的命令之一。它按顺序读取文件并将它们写入标准输出。你可以用它来查看文件内容、组合文件和创建新文件。

Basic Usage基本用法

$ cat file.txt
# Displays content of file.txt
$ cat file1.txt file2.txt > combined.txt
# Combines file1 and file2 into combined.txt
$ cat file.txt
# 显示file.txt的内容
$ cat file1.txt file2.txt > combined.txt
# 将file1和file2合并到combined.txt

Common Options常用选项

Option选项 Full Name完整名称 Description描述 Example示例
-n number行号 Number all output lines对所有输出行编号 cat -n file.txt
-b number-nonblank非空行号 Number non-blank lines对非空行编号 cat -b file.txt
-s squeeze-blank压缩空行 Suppress repeated empty lines压缩重复的空行 cat -s file.txt
-v show-nonprinting显示非打印字符 Show non-printing characters显示非打印字符 cat -v file.txt
-E show-ends显示行尾 Display $ at end of each line在每行末尾显示$ cat -E file.txt
-T show-tabs显示制表符 Display TAB characters as ^I将制表符显示为^I cat -T file.txt
-A show-all显示全部 Equivalent to -vET等同于-vET cat -A file.txt

Practical Scenarios实际场景

View Multiple Files at Once一次查看多个文件

$ cat header.txt content.txt footer.txt > complete.txt
# Creates complete.txt by combining three files
$ cat header.txt content.txt footer.txt > complete.txt
# 通过组合三个文件创建complete.txt

Create a New File创建新文件

$ cat > newfile.txt
Type your content here...
Press Ctrl+D to save and exit
$ cat > newfile.txt
在这里输入你的内容...
按Ctrl+D保存并退出

Append to Existing File追加到现有文件

$ cat additional.txt >> existing.txt
# Appends content without overwriting
$ cat additional.txt >> existing.txt
# 追加内容而不覆盖

Memory Tricks记忆技巧

🐱 "cat" = concatenate - Think of a cat linking multiple files together like a chain!

🐱 "cat" = concatenate(连接) - 想象一只猫像链条一样把多个文件连在一起!

🔢 -n = Number - Add line numbers to your output

🔢 -n = Number(数字) - 为输出添加行号

📦 -s = Squeeze - Squeeze multiple blank lines into one

📦 -s = Squeeze(挤压) - 将多个空行压缩成一个