11. 仅编号非空输出行

下面的命令只会对非空行进行编号,而不会对那些空白行进行编号。

$ cat -b /etc/login.defs 
     1	#
     2	# /etc/login.defs - Configuration control definitions for the login package.
     3	#
     4	# Three items must be defined:  MAIL_DIR, ENV_SUPATH, and ENV_PATH.
     5	# If unspecified, some arbitrary (and possibly incorrect) value will
     6	# be assumed.  All other items are optional - if not specified then
     7	# the described action or option will be inhibited.
     8	#
     9	# Comment lines (lines beginning with "#") and blank lines are ignored.
    10	#
    11	# Modified for Linux.  --marekm
    12	# REQUIRED for useradd/userdel/usermod
    13	#   Directory where mailboxes reside, _or_ name of file, relative to the
    14	#   home directory.  If you _do_ define MAIL_DIR and MAIL_FILE,
    15	#   MAIL_DIR takes precedence.

13.查看cat命令安装包的版本

下面的命令将向我们显示已安装的 cat 命令包版本、作者和许可证详细信息。

$ cat --version

有关使用此命令和所有可用参数的更多帮助,请参阅以下命令。

$ cat --help
Usage: cat [OPTION]... [FILE]...
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.
GNU coreutils online help: [http://www.gnu.org/software/coreutils/]
Full documentation at: [http://www.gnu.org/software/coreutils/cat]
or available locally via: info '(coreutils) cat invocation'

6. 将一个文件的内容转储到另一个文件

假设我们有一个包含一些内容的文件,并且我们想将所有内容复制到一个新文件中。
我们可以使用带有符号 >(大于)的 cat 命令来执行此操作。
这里我有一个名为 test1.txt 的文件,其中包含一些内容,我想将所有这些内容转储到一个名为 myfile.txt 的新文件中。

$ cat test1.txt > myfile.txt
# Output

$ cat myfile.txt
This is a test file....

3. Linux cat 命令和 less/more 命令一起使用 Pipe (|)

如果要显示无法在单屏下显示的长文件,请在管道 (|) 的帮助下使用带有命令 less 的 cat 命令。
参考下面的命令。

$ cat /etc/login.defs | less
之路教程 https://onitr oad .com

7. 将多个文件的内容转储到一个新文件中

我们还可以使用 Linux cat 命令将多个文件的内容转储到一个新文件中。
在这里,我将 test1.txt 和 test2.txt 的内容转储到名为 newfile.txt 的新文件中。

$ cat test1.txt test2.txt > newfile.txt
# Output
$ cat newfile.txt 
This is a test file....
Welcome to onitroad.com

2.显示多个文件的内容

如果要一次显示多个文件的内容,可以使用 cat 命令。
这里我有两个文件,例如:text1.txt 和 text2.txt。
那么让我们检查一下这两个文件的内容。

$ cat test1.txt test2.txt
This is a test file...
This is another test file...

8. 将内容添加到已创建的文件中(编辑文件)

我们可以使用带有符号 >>(双大于)的 cat 命令将内容(写入内容)添加到已创建的文件中。
运行以下命令后,我们必须键入要存储在该文件中的内容,然后按键盘上的 CTRL+D 保存并关闭文件。
参考下面的命令。

$ cat >> test1.txt

12. 显示 Tab 字符

我们可以使用带有参数 -T 的 Linux cat 命令来显示制表符。
所有包含“tab”的行都将表示为 ^I 。
参考下面的命令。

$ cat -T /etc/login.defs 
################# OBSOLETED BY PAM ##############
#^I^I^I^I^I^I#
# These options are now handled by PAM. Please^I#
# edit the appropriate file in /etc/pam.d/ to^I#
# enable the equivelants of them.
################## OBSOLETED #######################
#^I^I^I^I^I^I  #
# These options are no more handled by shadow.    #
#                                                 #
# Shadow utilities will display a warning if they #
# still appear.                                   #
#                                                 ## CLOSE_SESSIONS
# LOGIN_STRING
# NO_PASSWORD_CONSOLE
# QMAIL_DIR
Linux cat命令示例

在本文中,我们将学习如何使用 Linux cat 命令。
cat命令是concatenate的缩写。

cat 命令是 Linux 中的文件管理命令,用于显示文件内容、创建文件、编辑文件等。
它是 Linux 中最常用的基本命令之一。
Linux cat 命令是在 GNU GPLv3 许可下发布的开源应用程序。
默认情况下,它预装在任何 Linux 发行版中。
在本文中,我将通过示例向我们展示 14 个最重要的 Linux cat 命令。

cat 命令的语法是:

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

5. 创建一个新文件

我们可以使用带有符号 > (Greater Then) 的 Linux cat 命令创建一个新文件。
运行命令 (cat > test.txt) 后,我们必须输入要存储在该文件中的一些内容。
因此,键入一些文本,然后在键盘上按 CTRL+D 以创建并保存文件。

$ cat > test.txt
This is a test file...
ctrl+d

9. 编号所有输出行

我们可以使用带有参数 -n 的 Linux cat 命令对任何文件的所有输出行进行编号。
参考下面的命令。

$ cat -n /etc/passwd
     1	root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3	bin:x:2:2:bin:/bin:/usr/sbin/nologin
     4	sys:x:3:3:sys:/dev:/usr/sbin/nologin
     5	sync:x:4:65534:sync:/bin:/bin/sync
     6	games:x:5:60:games:/usr/games:/usr/sbin/nologin
     7	man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
     8	lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
     9	mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
    10	news:x:9:9:news:/var/spool/news:/usr/sbin/nologin

1. 显示文件内容

我们可以使用 Linux cat 命令显示文件的内容。
参考下面的命令。

$ cat test1.txt
This is a test file....

10.显示每行的结尾

带参数 -E 的 cat 命令将在每个输出行的末尾放置一个 $ 符号。
如果要验证每个输出行的结尾,此命令很有用。
请参考下面的示例命令。

$ cat -E /etc/passwd
root:x:0:0:root:/root:/bin/bash$
bin:x:1:1:bin:/bin:/sbin/nologin$
daemon:x:2:2:daemon:/sbin:/sbin/nologin$
adm:x:3:4:adm:/var/adm:/sbin/nologin$
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin$
sync:x:5:0:sync:/sbin:/bin/sync$
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown$
halt:x:7:0:halt:/sbin:/sbin/halt$
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin$
operator:x:11:0:operator:/root:/sbin/nologin$
games:x:12:100:games:/usr/games:/sbin/nologin$
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin$
nobody:x:99:99:Nobody:/:/sbin/nologin$
dbus:x:81:81:System message bus:/:/sbin/nologin$
...

4. 以相同的扩展名一次显示所有文件的内容

假设我们有许多具有相同扩展名的文件。
假设我有一些扩展名为 *.txt 的文本文件。
现在使用 Linux cat 命令显示这些文件的内容,如下所示。

$ cat *.txt
This is a test file....
Ubuntu 16.04 Long Term Support
Welcome to onitroad.com
日期:2020-09-17 00:14:56 来源:oir作者:oir