cat
cat
stands for concatenate and print files. It is an extremely useful tool that prints the entire contents of a file to standard output, by default. For example, to print the contents of a text file called myfile.txt
.
cat /home/john/myfile.txt
Hi! This is the text of /home/john/myfile.txt
cat
is particularly useful when we want to quickly check to see what is inside of a file. If, however, the file you are cat
-ing is a very large file, a tool like less
may be a better choice, as you can peek at the file and scroll in an efficient way, without dumping the entire contents.
cat
is commonly used to output the contents of a file and then pipe it to another tool, for data analysis, for example, that may not natively support reading from a file.
The nature of concatenating
files is when you print several files, for instance,
cat myfile1.txt myfile2.txt myfile3.txt myfile4.txt myfile5.txt >bigfile.txt
This will put the contents of these five files into bigfile.txt
.
The idea of concatenating
the files is that this new file contains the concatenated contents of the other five files, and the contents in the original five files are not changed.
Of course we can also append the contents of files to the end of other files. For instance,
cat myfile6.txt myfile7.txt myfile8.txt >>bigfile.txt
this appends these three files to the end of bigfile.txt
, so all eight files are now contained in bigfile.txt
. We emphasize that >
will overwrite the contents of a file (i.e., will destroy the file before writing to it), while >>
will append to the end of the current file, without destroying the contents that were already in the file beforehand.