5.2 将文件拼接至标准输出设备

cat file1 file2

cat是concatenate(拼接)的缩写,意思是“连接到一起”。cat命令最初的用途是将两个或更多的文件拼接成一个文件只对一个文件应用(cat命令并把其内容打印到屏幕上,属于设计之外的用法)。例如,假设你有一首A. E. Housman的短诗和一首Francis Quarles的短诗,想同时查看它们。

  1. $ cat housman_-_rue.txt quarles_-_the_world.txt
  2. WITH rue my heart is laden
  3. For golden friends I had,
  4. For many a rose-lipt maiden
  5. And many a lightfoot lad.
  6. By brooks too broad for leaping
  7. The lightfoot boys are laid;
  8. The rose-lipt girls are sleeping
  9. In fields where roses fade.
  10. The world's an Inn; and I her guest.
  11. I eat; I drink; I take my rest.
  12. My hostess, nature, does deny me
  13. Nothing, wherewith she can supply me;
  14. Where, having stayed a while, I pay
  15. Her lavish bills, and go my way.

注意,cat命令在显示时并没有用横线、破折号及类似的符号来分隔两个文件。实际上,cat命令将两个文件糅合在一起,再显示出来。如果想把文件隔开(短诗“With rue my heart is laden”最后一行跟着“On the world”的第一行很难阅读),需要确保每个拼接在一起的文件后面都留有一个空白行。