linux的cat命令使用

人气:370 ℃/2022-12-19 08:21:58

linux中cat命令有“查看一个文本文件的内容”、“将几个文本文件的内容串接在一起输出到某文件或者显示到终端”、“新建一个文件”三个常用用法,下面是具体介绍:

1、查看一个文本文件的内容,具体命令如下:

2、将几个文本文件的内容串接在一起输出到某文件或者显示到终端,具体命令如下:

3、新建一个文件,具体命令如下:

以上就是小编今天的分享了,希望可以帮助到大家。

linux中cat命令使用方法

通过16个实例掌握 Cat 命令 - Linux“撸猫”技巧

cat (concatenate) 命令是 Linux 和 Unix 中的基本命令之一。它用于创建新文件、连接文件,也用于在标准输出中查看文件的内容。

基本语法:

# cat <options> <File>

可用选项:

(1) 使用 cat 命令创建文件

假设我们要创建一个名为 linux_world 的新文件,键入 cat 命令,后跟要插入到文件中的文本内容,确保在最后键入 Ctrl D 保存文件。

[root@linuxtechi ~]# cat > linux_worldHi this is my first file in linux.Linux always rocksThanks[root@linuxtechi ~]#(2) 查看文件的内容

要使用 cat 命令查看现有文件的内容,示例如下:

[root@linuxtechi ~]# cat linux_worldHi this is my first file in linux.Linux always rocksThanksroot@linuxtechi ~]#(3) 查看多个文件的内容

要使用 cat 命令查看多个文件的内容,请在 cat 后面输入文件名 ,示例如下:

[root@linuxtechi ~]# cat linux_world linux_distributions /etc/fstab

上面的命令将在终端上显示三个文件的内容输出。

(4) 明智地查看内容页面

假如我们有一个大文件,其内容不能立即显示在屏幕上。在这种情况下,我们可以使用 cat 的 more 和 less 命令来明智地查看内容页。

[root@linuxtechi ~]# cat /etc/passwd | more[root@linuxtechi ~]# cat /etc/passwd | less(5) 不带文件名参数的 cat 命令

如果我们没有指定任何参数,那么它将从键盘读取输入。

[root@linuxtechi ~]# catUbuntu Linux Rocks at desktop Level

现在按 Ctrl d 通知 cat 已经到达文件结束 (EOF)。在这种情况下,它将显示一行文本两次,因为它将 std 输入复制到 std 输出。

[root@linuxtechi ~]# catUbuntu Linux Rocks at desktop LevelUbuntu Linux Rocks at desktop Level[root@linuxtechi ~]#(6) 使用行号查看内容

在 cat 命令中使用 -n 选项可以查看文件的内容和行号。

[root@linuxtechi ~]# cat -n linux_world1 Hi this is my first file in linux.2 Linux always rocks3 Thanks[root@linuxtechi ~]#

在这种情况下,如果你的文件有空行,那么上面的命令也会显示空行数,所以要删除空行编号,我们可以使用 -b 选项来代替上面命令中的 -n

(7) 将内容从一个文件复制到另一个文件

在 cat 命令中使用 > 符号,可以将一个文件的内容复制到另一个文件,示例如下:

[root@linuxtechi ~]# cat linux_world > linux_text[root@linuxtechi ~]#

在这种情况下,如果 linux_text 文件中有任何内容,那么它将被 linux_world 文件的内容覆盖

(8) 将一个文件的内容附加到另一个文件

在 cat 命令中使用 >> 符号,可以将一个文件的内容追加到另一个文件。示例如下:

[root@linuxtechi ~]# cat /etc/passwd >> linux_text[root@linuxtechi ~]#

上面的命令将在末尾将 /etc/passwd 文件的内容附加到 linux_text 文件

使用 cat 命令验证 linux_text 文件的内容

(9) 将多个文件的输出重定向到单个文件

cat 命令还可以用于将多个文件的内容合并为单个文件,示例如下:

[root@linuxtechi ~]# cat linux_world linux_distributions /etc/fstab > linux_merge_text

上面的命令将 3 个文件的输出合并为一个文件 linux_merge_text

(10) 使用标准输入操作符获取输入

在 cat 命令中使用 < 符号从标准输入操作符获取输入,示例如下:

[root@linuxtechi ~]# cat < linux_distributionsRHELCentOSFedoraUbuntuSuSELinux Mint[root@linuxtechi ~]#(11) 将多个文件排序输出为单个文件

cat 命令还可以对多个文件的内容进行排序,并将其合并为单个文件。默认情况下,排序将按照字母顺序进行,如果你想在数字的基础上排序,那么在 sort 命令中使用 -n 选项。

[root@linuxtechi ~]# cat linux_text linux_distributions /etc/passwd | sort > linux_sort(12) 在每一行的末尾插入 $

如果你想在文件中每一行的末尾附加 $ (dollar) 符号,那么在 cat 命令中使用 -E 选项。

[root@linuxtechi ~]# cat -E linux_worldHi this is my first file in linux.$Linux always rocks$Thanks$[root@linuxtechi ~]#(13) 显示内容中的制表符

如果要使用 cat 命令查看文件内容中的制表符,请使用 -T 选项,示例如下:

让我们创建一个带有制表符的文件

现在内容中的制表符将显示为 ^I

(14) 挤压重复的空白行

Use ‘-s’ option in cat command to suppress the repeated blank lines.

When we use -s option in cat command then it only show one blank line

and will squeeze repeated blank line.

在 cat 命令中使用 -s 选项禁止重复空行,它只显示一个空行,并将挤压重复的空行。

让我们以文件 linux_blank 为例,它由多个重复的空行组成

我们使用 cat 命令删除输出中重复的空行

[root@linuxtechi ~]# cat -s linux_blank testtest1test2test3test4[root@linuxtechi ~]#(15) 按倒序查看内容

tac 是 cat 反义词,tac 命令将以反转顺序显示输出,示例如下:

[root@linuxtechi ~]# tac linux_worldThanksLinux always rocksHi this is my first file in linux.[root@linuxtechi ~]#(16) 显示非打印字符

cat 命令中的 -v 选项用于显示非打印字符。当我们怀疑 CRLF 结束行时,这个选项变得非常有用,在这种情况下,它将在每行的末尾显示为 ^M

不带 -v 选项的内容输出

[root@linuxtechi tmp]# cat test_filehi there

带有 -v 选项的内容输出

[root@linuxtechi tmp]# cat -v test_filehi there^M[root@linuxtechi tmp]#我的开源项目

酷瓜云课堂-开源知识付费解决方案

推荐

首页/电脑版/网名
© 2026 NiBaKu.Com All Rights Reserved.