简介
cat命令就是用于查看内容较少的纯文本文件,当文件内容较大时,文本内容会在屏幕上快速闪动(滚屏),用户往往看不清所显示的具体内容。因此对于较长文件内容可以按Ctrl+S键,停止滚屏;以及Ctrl+Q键可以恢复滚屏;而按Ctrl+C(中断)键则可以终止该命令的执行。或者对于大文件,干脆用more命令吧!
命令格式
cat [OPTION]… [FILE]…
常用参数
-n | 显示行数(空行也编号) |
---|---|
-s | —squeeze-blank | 显示行数(多个空行算一个编号) |
-b | 显示行数(空行不编号) |
-E | 每行结束处显示$符号 |
-T | 将TAB字符显示为 ^I符号 |
-v | 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外 |
-e | 等价于”-vE”组合 |
-t |
等价于”-vT”组合 |
-A | 等价于 -vET组合 |
—help | 显示帮助信息 |
—version | 显示版本信息 |
实例
## 先默认查看一下源文件
[root@localhost ~]# cat anaconda-ks.cfg
# version=RHEL7
cdrom
# Run the Setup Agent on first boot
## -n : 显示行数(空行也编号)
[root@localhost ~]# cat -n anaconda-ks.cfg
1 # version=RHEL7
2
3
4 cdrom
5
6 # Run the Setup Agent on first boot
## -s : 显示行数(多个空行算一个编号)
[root@localhost ~]# cat -sn anaconda-ks.cfg
1 # version=RHEL7
2
3 cdrom
4
5 # Run the Setup Agent on first boot
## -b : 显示行数(空行不编号)
[root@localhost ~]# cat -b anaconda-ks.cfg
1 # version=RHEL7
2 cdrom
3 # Run the Setup Agent on first boot
## -E : 每行结束处显示$符号
[root@localhost ~]# cat -E anaconda-ks.cfg
# version=RHEL7$
$
$
cdrom$
$
# Run the Setup Agent on first boot$
## -T : 将TAB字符显示为 ^I符号
[root@localhost ~]# cat -T anaconda-ks.cfg
#^Iversion=RHEL7
cdrom
# Run the Setup Agent on first boot
其他用法
1. 查看文件内容并添加行号输出到另一个文件中
[root@localhost ~]# cat -n anaconda-ks.cfg
1 # version=RHEL7
2
3
4 cdrom
5
6 # Run the Setup Agent on first boot
[root@localhost ~]# cat -n anaconda-ks.cfg > test.txt
[root@localhost ~]# cat test.txt
1 # version=RHEL7
2
3
4 cdrom
5
6 # Run the Setup Agent on first boot
2. 清空一个文件的内容
[root@localhost ~]# cat test.txt
1 # version=RHEL7
2
3
4 cdrom
5
6 # Run the Setup Agent on first boot
[root@localhost ~]# cat /dev/null > test.txt
[root@localhost ~]# cat test.txt
[root@localhost ~]#
3. 持续写入文件内容,遇到 EOF 符后结束并保存
[root@localhost ~]# cat test.txt
[root@localhost ~]# cat > test.txt <<EOF
> hello
> EOF
[root@localhost ~]# cat test.txt
hello
4. 将多个文件合并成一个文件
[root@localhost ~]# cat test1.txt test2.txt
hello
world
[root@localhost ~]# cat test1.txt test2.txt > test3.txt
[root@localhost ~]# cat test3.txt
hello
world
5. 输出信息
[root@localhost ~]# cat
hello <== 输入完成回车,下面会自动输出你输入的内容
hello
world <== 同上
world
^C <== 结束执行 ctrl+c