Linux Cat命令与示例
CAT命令用于Linux以连接文件并在标准输出上打印。
CAT通常用于查看文件的内容,用于重定向,创建和连接文件。
默认情况下,CAT是所有Linux发行版中的内置命令。
cat命令的语法是:
cat [OPTION] [FILE]
1)查看文件的内容
要查看文件的内容,请使用cat命令后跟文件名。
例如:
$cat /etc/issue CentOS release 5.10 (Final) Kernel \r on an \m
要打开和查看多个文件内容,请使用Cat命令后跟多个文件路径:
$cat /etc/issue /etc/lsb-release Ubuntu 16.04.4 LTS \n \l DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS" root@testwebsrvlinode02:~# cat /etc/issue Ubuntu 16.04.4 LTS \n \l
2)复制到另一个文件
将内容复制到另一个文件,使用cat命令使用运算符>。
让我展示你的例子。
以下命令将创建具有与/root/linux文件相同的"linuxdistro"文件。
如果该文件已存在,它将重写内容。
$cat /root/linux > /root/linuxdistro
要从多个文件复制内容,请使用以下示例:
$cat file1.txt file2.txt > newfile.txt
3)将内容添加到文件末尾
如果我们希望将内容添加到文件使用运算符>>的末尾,而不是覆盖。
语法
$cat file1.txt file2.txt >> newfile3.txt
让我们看一个例子;
$cat file1.txt one two $cat file2.txt three $cat file1.txt file2.txt > newfile.txt $cat newfile.txt one two three $
4)创建一个新文件
有很多方法可以在Linux中创建文本文件。
要创建文件,请使用CAT命令后跟重定向运算符和文件名以创建。
在按ENTER键并添加文件内容,一旦完成按CRTL + D保存文件。
例如:
键入"Cat> Worke_System"时,它将创建一个名为Operating_System的文件。
然后按Enter键将在Cat命令下方看到一个空行。
我们可以键入所需的内容。
例如,我们键入了UNIX,Linux,Windows和MacoS。
完成后,按"Ctrl + D"以保存内容并退出Cat命令。
我们将看到一个名为'pecty_System'的文件在当前文件夹中创建了我们之前添加的内容。
# cat > operating_system Unix Linux Windows MacOS
注意:如果已存在"Working_System"文件,那么它将覆盖它。
5)重定向标准输入
我们还可以使用运算符<(较少符号)重定向标准输入。
在以下示例中,CAT命令的输入将是/root/linux的内容。
$cat < /root/linux
ubuntu centos redhat mint slackware
以下命令将采用文件/root/linux的内容,并将输出对文件进行排序:
$cat < /root/linux | sort > linux-sort centos mint redhat slackware ubuntu
6)将CAT命令与管子组合
将CAT命令与管道运算符组合起来非常有用。
让我们采取一些例子。
当文件无法适合屏幕时,可以使用更多或者更少的命令组合CAT |每页显示它。
# cat /proc/meminfo | less
# cat /proc/meminfo | more
使用CAT命令使用SED命令删除特定行号,如下:
以下命令从名为sample.txt中的文件中删除行号3:
$cat -n /home/sample.txt | sed '3d' 1 text 1 2 text 2 4 text 4 5 text 5 6 text 6
如果要对其进行排序,可以使用sort命令组合它。
这是一个例子:
$cat /root/linux | sort centos mint redhat slackware Ubuntu
7)将线路编号添加到文件
要将行号添加到文件中,请使用带有-n选项的cat命令,然后使用filename。
例如:
# cat -n /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 6 # Permit all access over the loopback interface. This could be 7 # tightened as well, but to do so would effect some of the 8 # administration functions 9 restrict 127.0.0.1 10 restrict -6 ::1
8)数字非空白输出线
与-n选项类似,-b选项将在文件中显示行号。
区别在于-b选项只会编号非空行。
示例输出:
$cat -b /etc/ntp.conf 1 # Permit time synchronization our time resource but do not 2 # permit the source to query or modify the service on this system 3 restrict default kod nomodify notrap nopeer noquery 4 restrict -6 default kod nomodify notrap nopeer noquery 5 # Permit all access over the loopback interface. This could be 6 # tightened as well, but to do so would effect some of the 7 # administration functions 8 restrict 127.0.0.1 9 restrict -6 ::1
9)删除重复的空线
要抑制重复的空输出行使用-s选项。
这将在文件中挤压多行。
即不会超过一个空线。
$cat /home/sample.txt text 1 text 2 text 4 text 5 $cat -s /home/sample.txt text 1 text 2 text 4 text 5
10)在文件中显示选项卡
要在文件中指示选项卡,请使用选项-t。
选项卡将由^ i字符表示。
# cat -T /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 ^I^I localhost.localdomain localhost ::1 ^I^I localhost6.localdomain6 localhost6
11)显示线条
使用-e参数显示每行结尾的'$'。
这是一个例子:
# cat -E /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1 localhost.localdomain localhost$ ::1 localhost6.localdomain6 localhost6$
如果要在-t和-e之间组合,则可以使用-a参数。
# cat -A /etc/hosts # Do not remove the following line, or various programs$ # that require network functionality will fail.$ 127.0.0.1^I^Ilocalhost.localdomain localhost$ ::1^I^Ilocalhost6.localdomain6 localhost6$
12)读取内容,直到特定模式
当我们从Stdin读取时,我们可以读取,直到包含特定模式的行。
在下面的示例中,从STDIN(直到EOF)读取行块并在标准输出上打印。
# cat <<EOF > Redhat > Ubuntu > EOF
和上述输出将是:
Redhat Ubuntu
13)使用通配符显示所有文件
将Cat命令与Linux通配符组合,以在当前目录中查看文件的文本。
以下命令将显示当前目录中的每个文件中的所有文本。
# cat *
或者
显示当前目录中具有扩展名的每个文件的所有文本。
# cat *.txt