---
title: "cat 命令详解"
author: "大孩爱小孩"
date: "2017-03-04T13:08:14Z"
tags: ["Linux", "OS", "Unix", "cat"]
categories: ["操作系统"]
slug: "cat"
draft: false
lastmod: "2017-03-07T21:24:06Z"
id: 2360248
url: "https://lshj.iteye.com/blog/2360248"
---

**Linux -- cat 查看文件内容**

 

CAT(1)                      User Commands                      CAT(1)

 

**NAME 名称**

    cat - concatenate files and print on the standard output

          连接文件，将文件内容显示在标准输出上

 

**SYNOPSIS 语法**

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

 

**DESCRIPTION 说明**

    Concatenate FILE(s), or standard input, to standard output.

    连接文件，或标准输入，显示在标准输出上。

 

    -A, --show-all

        equivalent to -vET

        等同于-vET。

 

    -b, --number-nonblank

        number nonempty output lines, overrides -n

        对非空行进行编号，同-n一起使用时，-n无效。

 

    -e  equivalent to -vE

        等同于-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

        等同于-vT。

 

    -T, --show-tabs

        display TAB characters as ^I

        将TAB字符用^I显示。

 

    -u  (ignored)

        忽略。

 

    -v, --show-nonprinting

        use ^ and M- notation, except for LFD and TAB

        除LFD和TAB之外，使用^和M-字符显示非ASCII字符。

 

    --help display this help and exit

        显示帮助信息并退出。

 

    --version

        output version information and exit

        显示版本信息并退出。

 

    With no FILE, or when FILE is -, read standard input.

    当没有指定文件或文件占位符用'-'代替，则从标准输入读取。

 

**EXAMPLES 举例**

    cat f - g

        Output f's contents, then standard input, then g's contents.

        输出f的内容，然后是从标准输入设备读取的内容，最后是g的内容。

 

    cat    Copy standard input to standard output.

        输出从标准输入设备读取的内容到标准输出。

 

    GNU coreutils online help: &lt;http://www.gnu.org/software/coreutils/&gt;

    Report cat translation bugs to &lt;http://translationproject.org/team/&gt;

 

**AUTHOR 作者**

    Written by Torbjorn Granlund and Richard M. Stallman.

 

**COPYRIGHT 版权**

    Copyright (C) 2013 Free Software Foundation, Inc.  License GPLv3+: GNU  GPL  version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;.

    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)

 

    The full documentation for cat is maintained as a Texinfo manual. If the info and cat programs are properly installed at your site, the command

        info coreutils 'cat invocation'

    should give you access to the complete manual.

    cat的完整文档是以Texinfo手册形式维护的。如果info和cat程序都已经安装，那么执行命令：

        info coreutils 'cat invocation'

    应该会让你访问到整篇手册。

 

GNU coreutils 8.22            June 2014            CAT(1)

 

**案例分析：**

**一、原始文件及内容**

```
[root@user test]# ls
test.txt
[root@user test]# cat test.txt
abcdefghijklmnopqrstuvwxyz
cdefghijklm nopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc

efghijklmnopqrstuvwxyzabcd


ijklmnopqrstuvwxyzabcdefgh
9ijklmnopqrstuvwxyzabcdefgh

瞄瞄
呀
sadfsdf9    00-=-+!@#$%^&
■▲△→↑↓￣︿＠
[root@user test]#
```

 

**二、查看test.txt内容及行号**

```
[root@user test]# cat -n test.txt
     1  abcdefghijklmnopqrstuvwxyz
     2  cdefghijklm nopqrstuvwxyzab
     3  defghijklmnopqrstuvwxyzabc
     4
     5  efghijklmnopqrstuvwxyzabcd
     6
     7
     8  ijklmnopqrstuvwxyzabcdefgh
     9  9ijklmnopqrstuvwxyzabcdefgh
    10
    11  瞄瞄
    12  呀
    13  sadfsdf9    00-=-+!@#$%^&
    14  ■▲△→↑↓￣︿＠
[root@user test]#
```

 

**三、查看test.txt内容及行号，空行不编号**

```
[root@user test]# cat -b test.txt
     1  abcdefghijklmnopqrstuvwxyz
     2  cdefghijklm nopqrstuvwxyzab
     3  defghijklmnopqrstuvwxyzabc

     4  efghijklmnopqrstuvwxyzabcd


     5  ijklmnopqrstuvwxyzabcdefgh
     6  9ijklmnopqrstuvwxyzabcdefgh
     7
     8  瞄瞄
     9  呀
    10  sadfsdf9    00-=-+!@#$%^&
    11  ■▲△→↑↓￣︿＠
[root@user test]#
```

 

**四、查看test.txt内容及行号，多空行仅显示一行**

```
[root@user test]# cat -sn test.txt
     1  abcdefghijklmnopqrstuvwxyz
     2  cdefghijklm nopqrstuvwxyzab
     3  defghijklmnopqrstuvwxyzabc
     4
     5  efghijklmnopqrstuvwxyzabcd
     6
     7  ijklmnopqrstuvwxyzabcdefgh
     8  9ijklmnopqrstuvwxyzabcdefgh
     9
    10  瞄瞄
    11  呀
    12  sadfsdf9    00-=-+!@#$%^&
    13  ■▲△→↑↓￣︿＠
[root@user test]#
```

 

**五、查看test.txt内容及行号，多空行仅显示一行，TAB和每行结尾加标识**

```
[root@user test]# cat -snTE test.txt
     1  abcdefghijklmnopqrstuvwxyz$
     2  cdefghijklm^Inopqrstuvwxyzab$
     3  defghijklmnopqrstuvwxyzabc$
     4  $
     5  efghijklmnopqrstuvwxyzabcd$
     6  $
     7  ijklmnopqrstuvwxyzabcdefgh$
     8  9ijklmnopqrstuvwxyzabcdefgh$
     9  ^I$
    10  瞄瞄$
    11  呀$
    12  sadfsdf9^I00-=-+!@#$%^&$
    13  ■▲△→↑↓￣︿＠$
[root@user test]#
```

 

**六、-v显示**

 

```
[root@user test]# cat -v test.txt
abcdefghijklmnopqrstuvwxyz
cdefghijklm nopqrstuvwxyzab
defghijklmnopqrstuvwxyzabc

efghijklmnopqrstuvwxyzabcd


ijklmnopqrstuvwxyzabcdefgh
9ijklmnopqrstuvwxyzabcdefgh

M-gM-^^M-^DM-gM-^^M-^D
M-eM-^QM-^@
sadfsdf9    00-=-+!@#$%^&
M-bM-^VM- M-bM-^VM-2M-bM-^VM-3M-bM-^FM-^RM-bM-^FM-^QM-bM-^FM-^SM-oM-?M-#M-oM-8M-?M-oM-<M-
[root@user test]# 
```