如何在 Linux 中使用 ‘cat’ 和 ‘tac’ 命令及示例

本文是我们的Linux 技巧与提示系列的一部分,在本文中,我们将介绍cat命令的一些基本用法(Linux 中最常用的命令之一)和tac(cat 命令的反向操作 – 以相反顺序打印文件)并附带一些实际示例。

阅读更多: Linux 中有用的 ‘cat’ 命令示例

Linux 中 cat 命令的基本用法

Cat命令,代表Concatenate,是*nix 系统中最常用的命令之一。该命令的最基本用法是读取文件并将它们显示到stdout,即在终端上显示文件的内容。

# cat file.txt
View Content of File in Linux

cat命令的另一个用法是读取或合并多个文件,并将输出发送到监视器,如下面的示例所示。

# cat file1.txt file2.txt file3.txt
View Content of Multiple Files

该命令还可用于使用“>” Linux 重定向运算符将多个文件连接(合并)为一个单一文件。

# cat file1.txt file2.txt file3.txt > file-all.txt
Join Multiple Files in Linux

通过使用追加重定向器,您可以使用以下语法将新文件的内容添加到file-all.txt的底部。

# cat file4.txt >> file-all.txt
Append Content File to New File

cat 命令可用于将文件内容复制到新文件中。新文件的名称可以任意更改。例如,将文件从当前位置复制到/tmp/目录。

# cat file1.txt > /tmp/file1.txt 
Copy Content of File to New File

将文件从当前位置复制到/tmp/目录并更改其名称。

# cat file1.txt > /tmp/newfile.cfg
Copy File to /tmp Location

A less usage of the cat command is to create a new file with the below syntax. When finished editing the file hit CTRL+D to save and exit the new file.

# cat > new_file.txt
Create New File using Cat Command

为了对文件的所有输出行进行编号,包括空行,请使用-n开关。

# cat -n file-all.txt
Add Numbers to Lines in File

要仅显示每个非空行的编号,请使用-b开关。

# cat -b file-all.txt
Print Line Numbers in File

想了解更多关于Linux cat命令的信息吗?那就阅读我们的文章:Linux中13个有用的“cat”命令示例

学习如何在Linux中使用Tac命令

另一方面,在*nix系统中较少为人知且较少使用的命令是tac命令。Tac实际上是cat命令的反向版本(也是反向拼写),它会将文件的每一行从底部开始打印到顶部行,输出到您的机器标准输出。

# tac file-all.txt
Print Content File in Reverse Order

该命令最重要的选项之一是-s开关,它基于文件中的字符串或关键字来分隔文件的内容。

# tac file-all.txt --separator "two"
Remove Matching String in File

接下来,tac命令的最重要用途是,在调试日志文件时提供很大帮助,可以颠倒日志内容的时间顺序。

$ tac /var/log/auth.log

Or to display the last lines

$ tail /var/log/auth.log | tac
示例输出
tecmint@tecmint ~ $ tac /var/log/auth.log
pr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
....
tecmint@tecmint ~ $ tail /var/log/auth.log | tac
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session closed for user root
Apr  6 16:09:01 tecmint CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session closed for user root
Apr  6 16:05:01 tecmint CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:05:01 tecmint CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session closed for user root
Apr  6 16:00:01 tecmint CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr  6 15:55:02 tecmint CRON[17194]: pam_unix(cron:session): session closed for user root
Apr  6 15:55:01 tecmint CRON[17195]: pam_unix(cron:session): session closed for user root
...

cat命令相同,tac处理文本文件方面表现出色,但应避免在其他类型的文件中使用,特别是二进制文件或在第一行表示将运行它的程序的文件中。

Source:
https://www.tecmint.com/learn-linux-cat-command-and-tac-command/