Wednesday, October 7, 2009

Linux - Concatenating and Appending Files

Linux - Concatenating and Appending Files

Concatenating Files

Cat can read input from multiple input files and it will send its output to stdout,

$ cat file1
cookies
pizza
$ cat file2
dogs
cats
$ cat file3
red blue
$ cat file1 file2 file3 > file4
$ cat file4
cookies
pizza
dogs
cats
red blue

Here, the contents of the files file1, file2, and file3 have been concatenated together when creating the new file file4. Now you can see where the name cat came from (it is short for concatenate)

Appending to a File

To append a file to another file use the >> symbol,

$ cat file1
cookies
pizza
$ cat file2
dogs
cats
$ cat file1 file2 >> file1
$ cat file1
cookies
pizza
cookies
pizza
dogs
cats

The above program is adding the contents of file1 and file2 to the end of file1.

The notation >> is equivalent to 1>> and the notation 2>> can be used to concatenate to stderr. 

 

No comments:

Post a Comment