From Knoppix Documentation Wiki
Jump to: navigation, search

cat: Concatenate

cat filename... where filename... may be one or more files. cat displays the content of filename... on the standard output. If more than one file is used, they are displayed one after the other creating a larger file.

Output redirection may be used to copy the contents of files into another file. An example is

cat *.c > tot.cprog

This will copy the contents of all files ending with .c, into file tot.cprog. The order of the files within tot.cprog will be alphabetical.

cat can be used to append one file to another:

cat file2 >> file1

will append file2 to file1.

The cat command can also be used in pipe sequences or used to create files in shell scripts.

cat - > mynewfile.txt << CATEOF
line 1
line 2
line 3

The -, dash, is used to take input from the keyboard or in the case of this shell script example from all the lines following the cat line. Notice how << CATEOF' is used. The << tells the shell script to that input is going to be taking from lines in the shell script while the -, dash, tells cat to read from the stdin. CATEOF is just some sequence of characters that you invent. The token name means cat End Of File. In this example, the script starts to look for a matching CATEOF search token. The closing search token must start at the first character on the line.