linux命令之cat
一.cat命令基础
cat命令主要用来查看文件内容,创建文件,文件合并,追加文件内容等功能。
cat命令全称:concatenate files and print on the standard output
语法
cat (参数)(选项)
-n 或 --number:由 1 开始对所有输出的行数编号。
-b 或 --number-nonblank:和 -n 相似,只不过对于空白行不编号。
-s 或 --squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。
-v 或 --show-nonprinting:使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外。
-E 或 --show-ends : 在每行结束处显示 $。
-T 或 --show-tabs: 将 TAB 字符显示为 ^I。
-e : 等价于 -vE。
-A, --show-all:等价于 -vET。
-t:等价于"-vT"选项;
二.cat的四种用法
1.:查看文件内容主要用法:
1、cat 1.txt,查看1.txt文件的内容。
[root@k8s-master01 ~]# cat 1.txt
cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.244.0.1 netmask 255.255.255.0 broadcast 10.244.0.255
inet6 fe80::d0bd:39ff:fe20:a7cf prefixlen 64 scopeid 0x20<link>
ether d2:bd:39:20:a7:cf txqueuelen 1000 (Ethernet)
RX packets 62744 bytes 4294453 (4.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64896 bytes 19520211 (18.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2、cat -n 1.txt,查看1.txt文件的内容,并且由1开始对所有输出行进行编号。
[root@k8s-master01 ~]# cat -n 1.txt
1 cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
2 inet 10.244.0.1 netmask 255.255.255.0 broadcast 10.244.0.255
3 inet6 fe80::d0bd:39ff:fe20:a7cf prefixlen 64 scopeid 0x20<link>
4 ether d2:bd:39:20:a7:cf txqueuelen 1000 (Ethernet)
5 RX packets 62744 bytes 4294453 (4.0 MiB)
6 RX errors 0 dropped 0 overruns 0 frame 0
7 TX packets 64896 bytes 19520211 (18.6 MiB)
8 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
9
3、cat -b 1.txt,查看1.txt文件的内容,用法与-n相似,只不过对于空白行不编号。
[root@k8s-master01 ~]# cat -b 1.txt
1 cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
2 inet 10.244.0.1 netmask 255.255.255.0 broadcast 10.244.0.255
3 inet6 fe80::d0bd:39ff:fe20:a7cf prefixlen 64 scopeid 0x20<link>
4 ether d2:bd:39:20:a7:cf txqueuelen 1000 (Ethernet)
5 RX packets 62744 bytes 4294453 (4.0 MiB)
6 RX errors 0 dropped 0 overruns 0 frame 0
7 TX packets 64896 bytes 19520211 (18.6 MiB)
8 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4、cat -s 1.txt,当遇到有连续两行或两行以上的空白行,就代换为一行的空白行。
5、cat -e f.txt,在输出内容的每一行后面加一个$符号。
6、cat 1.txt 2.txt,同时显示1.txt和2.txt文件内容,注意文件名之间以空格分隔,而不是逗号。
[root@k8s-master01 ~]# cat 1.txt load.sh
cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.244.0.1 netmask 255.255.255.0 broadcast 10.244.0.255
inet6 fe80::d0bd:39ff:fe20:a7cf prefixlen 64 scopeid 0x20<link>
ether d2:bd:39:20:a7:cf txqueuelen 1000 (Ethernet)
RX packets 62744 bytes 4294453 (4.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64896 bytes 19520211 (18.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#!/bin/bash
ls /root/kubeadm-basic.images >> /tmp/images.tmp
for i in $(cat /tmp/images.tmp)
do
docker load -i /root/kubeadm-basic.images/$i
done
7、cat -n f1.txt>f2.txt,对1.txt文件中每一行加上行号后然后写入到2.txt中,会覆盖原来的内容,文件不存在则创建它。(覆盖写入)
8、cat -n 1.txt>>2.txt,对f1.txt文件中每一行加上行号后然后追加到f2.txt中去,不会覆盖原来的内容,文件不存在则创建它。(追加写入)
2.创建文件以及写入文件内容的用法
:
注意:创建文件的时候要设置文件结束标志,也就是<<EOF,可以把EOF换成别的字符,注意是大小写敏感的,当文件内容写完之后要输入结束标志EOF,这时命令会正确结束,表示成功创建文件并且写进内容。
[root@k8s-master01 ~]# cat << EOF > test.sh
```powershell
> #!bin/bash
> echo "huliqiang is so handsome"
> EOF
[root@k8s-master01 ~]# cat test.sh
#!bin/bash
echo "huliqiang is so handsome"
[root@k8s-master01 ~]# chmod a+x test.sh
[root@k8s-master01 ~]# bash test.sh
huliqiang is so handsome
C:追加文件内容的用法:
注意:与创建文件内容不同的是符号单边号>变成了双边号>>。
[root@k8s-master01 ~]# cat << EOF >> test.sh
> echo ”luoshan :yes ,he is!“
> EOF
[root@k8s-master01 ~]# cat test.sh
#!bin/bash
echo "huliqiang is so handsome"
echo ”luoshan :yes ,he is!“
[root@k8s-master01 ~]# bash test.sh
huliqiang is so handsome
”luoshan :yes ,he is!“
D:文件合并的用法
把文件2.txt,3.txt,4.txt的文件内容写入到1.txt中,如果1.txt文件以前有内容,则先会清除它们然后再写入合并后的内容。
如果不想清除文件内容,则可以把单边号>变成了双边号>>
[root@k8s-master01 ~]# cat 2.txt
cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
inet 10.244.0.1 netmask 255.255.255.0 broadcast 10.244.0.255
inet6 fe80::d0bd:39ff:fe20:a7cf prefixlen 64 scopeid 0x20<link>
ether d2:bd:39:20:a7:cf txqueuelen 1000 (Ethernet)
RX packets 62744 bytes 4294453 (4.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64896 bytes 19520211 (18.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#!bin/bash
echo "huliqiang is so handsome"
echo ”luoshan :yes ,he is!“