楔子:作为一名经常开车的老司机,查看日志文件是一种家常便饭。Linux more 命令就是这样一道非常下饭的菜,学会用它查看日志文件,也许会让很多问题及时被发现和定位。
Linux cat 命令
Linux cat 命令,用于连接文件并打印到标准输出设备上。与 tac 相反,cat 是从第一行到最后一行连续显示在萤幕上。
语法格式
cat [-选参] [--help] [--version] fileName
选参说明
可选参数简写 | 可选参数全称 | 含义 |
---|---|---|
-n | --number | 由 1 开始对所有输出的行数编号 |
-b | --number-nonblank | 和 -n 相似,只不过对于空白行不编号 |
-s | --squeeze-blank | 当遇到有连续两行以上的空白行,就代换为一行的空白行 |
-v | --show-nonprinting | 使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外 |
-E | --show-ends | 在每行结束处显示 $ |
-T | --show-tabs | 将 TAB 字符显示为 ^I |
-A | --show-all | 等价于 -vET 选项 |
-e | 等价于 -vE 选项 | |
-t | 等价于 -vT 选项 | |
--help | 显示帮助并退出 | |
--version | 显示版本信息并退出,没有指定文件或指定的文件是 -,则从标准输入读取 |
参考示例
01)创建新文件
[devuser@sh-k82 ~]$ cat > test.txt << EOF
> hello cat
> cat cat cat
> it is not cat
>
> test
>
> EOF
02)查看文件的内容
[devuser@sh-k82 ~]$ cat test.txt
hello cat
cat cat cat
it is not cat
test
03)查看文件的内容,并显示行数编号
[devuser@sh-k82 ~]$ cat -n test.txt
1 hello cat
2 cat cat cat
3 it is not cat
4
5 test
6
04)查看文件的内容,并添加行数编号后输出到另外一个文件中
[devuser@sh-k82 ~]$ cat -n test.txt > test2.txt
[devuser@sh-k82 ~]$ ll
total 8
-rw-rw-r-- 1 devuser devuser 98 Oct 9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct 9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test2.txt
1 1 hello cat
2 2 cat cat cat
3 3
4 4 it is not that cat
5 5
6 6 test
7 7
05)复制文件
[devuser@sh-k82 ~]$ cat test.txt > test3.txt
[devuser@sh-k82 ~]$ ll
total 12
-rw-rw-r-- 1 devuser devuser 98 Oct 9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct 9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 49 Oct 9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test3.txt
1 hello cat
2 cat cat cat
3
4 it is not that cat
5
6 test
7
[devuser@sh-k82 ~]$
06)清空文件内容
[devuser@sh-k82 ~]$ cat /dev/null test.txt > test.txt
cat: test.txt: input file is output file
[devuser@sh-k82 ~]$ cat test.txt
[devuser@sh-k82 ~]$
注:dev/null:在类 Unix 系统中,/dev/null 称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一个 EOF。
而使用 cat $filename > /dev/null 则不会得到任何信息,因为我们将本来该通过标准输出显示的文件信息重定向到了 /dev/null 中。
使用 cat $filename 1 > /dev/null 也会得到同样的效果,因为默认重定向的 1 就是标准输出。如果你对 shell 脚本或者重定向比较熟悉的话,应该会联想到 2,也即标准错误输出。
如果我们不想看到错误输出呢?我们可以禁止标准错误 cat $badname 2 > /dev/null。
07)持续写入文件内容,碰到 END 符后自动结束并保存文件
[devuser@sh-k82 ~]$ cat > test.txt << END
> hello cat
>
>
> cat cat cat
>
>
> it is not that cat
>
> test
> END
[devuser@sh-k82 ~]$ cat test.txt
hello cat
cat cat cat
it is not that cat
test
[devuser@sh-k82 ~]$
08)对非空行进行编号,空行不编号进行输出打印
[devuser@sh-k82 ~]$ cat -b test.txt
1 hello cat
2 cat cat cat
3 it is not that cat
4 test
[devuser@sh-k82 ~]$
09)有时候文件中空行会很多,需要将多个空行合并为一行进行输出(不会改变文件内容)
[devuser@sh-k82 ~]$ cat -s test.txt
hello cat
cat cat cat
it is not that cat
test
[devuser@sh-k82 ~]$
10)在每行结束处显示 $
[devuser@sh-k82 ~]$ cat -E test.txt
hello cat$
$
$
cat cat cat$
$
$
it is not that cat$
$
test$
[devuser@sh-k82 ~]$
11)合并多个文件内容到一个新文件
[devuser@sh-k82 ~]$ cat test2.txt test3.txt > test4.txt
[devuser@sh-k82 ~]$ ll
total 16
-rw-rw-r-- 1 devuser devuser 98 Oct 9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct 9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 147 Oct 9 23:10 test4.txt
-rw-rw-r-- 1 devuser devuser 51 Oct 9 22:50 test.txt
[devuser@sh-k82 ~]$ cat test4.txt
1 hello cat
2 cat cat cat
3
4 it is not that cat
5
6 test
7
hello cat
cat cat cat
it is not that cat
test
[devuser@sh-k82 ~]$
12)制作镜像文件。例如要制作软盘的镜像文件,将软盘放好后输入:
cat /dev/fd0 > OUTFILE
注:OUTFILE 指输出的镜像文件名