回到手册索引

命令用途

cat(concatenate)是一个非常常用的 Linux/Unix 命令,主要用于将一个或多个文件的内容连接并输出到标准输出(通常是终端)。它常用于查看文件内容、创建文件、合并文件等操作。

常用用法示例

  1. 显示单个文件的内容

    1
    cat file1.txt

    打印 file1.txt 文件的内容。

  2. 连接多个文件并输出

    1
    cat file1.txt file2.txt

    先输出 file1.txt 的内容,再输出 file2.txt 的内容。

  3. 将标准输入内容显示到标准输出

    1
    cat

    读取标准输入的内容,按行输出。按 Ctrl+D 结束输入。

  4. 将多个文件的内容合并输出并重定向到新文件

    1
    cat file1.txt file2.txt > combined.txt

    将 file1.txt 和 file2.txt 的内容合并并写入到 combined.txt 文件中。

  5. 在输出中显示行号

    1
    cat -n file1.txt

    显示 file1.txt 内容并为每行添加行号。

  6. 显示行尾的 $ 符号

    1
    cat -E file1.txt

    在每行的末尾显示 $ 符号,以标识行尾。

  7. 抑制重复的空行

    1
    cat -s file1.txt

    合并文件中的空行,避免重复的空行被显示。

  8. 将制表符显示为 ^I

    1
    cat -T file1.txt

    将文件 file1.txt 中的制表符(TAB)显示为 ^I。

常用参数选项

  • -A, –show-all
    等同于 -vET,显示不可打印字符、显示行尾符号和制表符。

  • -b, –number-nonblank
    为非空输出行编号,覆盖 -n 参数的作用。

  • -e
    等同于 -vE,显示不可打印字符并显示行尾符号。

  • -E, –show-ends
    在每行的末尾显示 $,用于标识行尾。

  • -n, –number
    为输出的每一行添加行号。

  • -s, –squeeze-blank
    抑制重复的空行,将多个连续的空行合并为一行。

  • -T, –show-tabs
    将制表符显示为 ^I。

  • -v, –show-nonprinting
    显示所有不可打印字符,使用 ^ 和 M- 符号表示,除了换行符和制表符。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
NAME 

cat - concatenate files and print on the standard output

SYNOPSIS

cat [OPTION]... [FILE]...

DESCRIPTION

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.

AUTHOR

Written by Torbjorn Granlund and Richard M. Stallman.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

tac(1)

Full documentation <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found athttp://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
http://ftp.gnu.org/gnu/coreutils/⟩ on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org