5.2 将文件拼接至标准输出设备
cat file1 file2
cat
是concatenate(拼接)的缩写,意思是“连接到一起”。cat
命令最初的用途是将两个或更多的文件拼接成一个文件只对一个文件应用(cat
命令并把其内容打印到屏幕上,属于设计之外的用法)。例如,假设你有一首A. E. Housman的短诗和一首Francis Quarles的短诗,想同时查看它们。
$ cat housman_-_rue.txt quarles_-_the_world.txt
WITH rue my heart is laden
For golden friends I had,
For many a rose-lipt maiden
And many a lightfoot lad.
By brooks too broad for leaping
The lightfoot boys are laid;
The rose-lipt girls are sleeping
In fields where roses fade.
The world's an Inn; and I her guest.
I eat; I drink; I take my rest.
My hostess, nature, does deny me
Nothing, wherewith she can supply me;
Where, having stayed a while, I pay
Her lavish bills, and go my way.
注意,cat
命令在显示时并没有用横线、破折号及类似的符号来分隔两个文件。实际上,cat
命令将两个文件糅合在一起,再显示出来。如果想把文件隔开(短诗“With rue my heart is laden”最后一行跟着“On the world”的第一行很难阅读),需要确保每个拼接在一起的文件后面都留有一个空白行。