简介

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 显示版本信息

实例

  1. ## 先默认查看一下源文件
  2. [root@localhost ~]# cat anaconda-ks.cfg
  3. # version=RHEL7
  4. cdrom
  5. # Run the Setup Agent on first boot
  6. ## -n : 显示行数(空行也编号)
  7. [root@localhost ~]# cat -n anaconda-ks.cfg
  8. 1 # version=RHEL7
  9. 2
  10. 3
  11. 4 cdrom
  12. 5
  13. 6 # Run the Setup Agent on first boot
  14. ## -s : 显示行数(多个空行算一个编号)
  15. [root@localhost ~]# cat -sn anaconda-ks.cfg
  16. 1 # version=RHEL7
  17. 2
  18. 3 cdrom
  19. 4
  20. 5 # Run the Setup Agent on first boot
  21. ## -b : 显示行数(空行不编号)
  22. [root@localhost ~]# cat -b anaconda-ks.cfg
  23. 1 # version=RHEL7
  24. 2 cdrom
  25. 3 # Run the Setup Agent on first boot
  26. ## -E : 每行结束处显示$符号
  27. [root@localhost ~]# cat -E anaconda-ks.cfg
  28. # version=RHEL7$
  29. $
  30. $
  31. cdrom$
  32. $
  33. # Run the Setup Agent on first boot$
  34. ## -T : 将TAB字符显示为 ^I符号
  35. [root@localhost ~]# cat -T anaconda-ks.cfg
  36. #^Iversion=RHEL7
  37. cdrom
  38. # Run the Setup Agent on first boot

其他用法

  1. 1. 查看文件内容并添加行号输出到另一个文件中
  2. [root@localhost ~]# cat -n anaconda-ks.cfg
  3. 1 # version=RHEL7
  4. 2
  5. 3
  6. 4 cdrom
  7. 5
  8. 6 # Run the Setup Agent on first boot
  9. [root@localhost ~]# cat -n anaconda-ks.cfg > test.txt
  10. [root@localhost ~]# cat test.txt
  11. 1 # version=RHEL7
  12. 2
  13. 3
  14. 4 cdrom
  15. 5
  16. 6 # Run the Setup Agent on first boot
  17. 2. 清空一个文件的内容
  18. [root@localhost ~]# cat test.txt
  19. 1 # version=RHEL7
  20. 2
  21. 3
  22. 4 cdrom
  23. 5
  24. 6 # Run the Setup Agent on first boot
  25. [root@localhost ~]# cat /dev/null > test.txt
  26. [root@localhost ~]# cat test.txt
  27. [root@localhost ~]#
  28. 3. 持续写入文件内容,遇到 EOF 符后结束并保存
  29. [root@localhost ~]# cat test.txt
  30. [root@localhost ~]# cat > test.txt <<EOF
  31. > hello
  32. > EOF
  33. [root@localhost ~]# cat test.txt
  34. hello
  35. 4. 将多个文件合并成一个文件
  36. [root@localhost ~]# cat test1.txt test2.txt
  37. hello
  38. world
  39. [root@localhost ~]# cat test1.txt test2.txt > test3.txt
  40. [root@localhost ~]# cat test3.txt
  41. hello
  42. world
  43. 5. 输出信息
  44. [root@localhost ~]# cat
  45. hello <== 输入完成回车,下面会自动输出你输入的内容
  46. hello
  47. world <== 同上
  48. world
  49. ^C <== 结束执行 ctrl+c