There's a neat twist to output redirection which allows you to add new information to the end of an existing file. Similar to when you used the > symbol, you tell your shell to send the information somewhere other than standard output.
However, when you use >>, you're adding information, rather than replacing it.
The best explanation is a demonstration, so let's take two files which have already been created -- sneakers.txt and home.txt -- and join them by using the append output symbol. We want to add the information in home.txt to the information already in sneakers.txt, so we type:
cat home.txt >> sneakers.txt |
Now let's check the file by typing:
cat sneakers.txt |
And there it is -- with the contents of home.txt at the end.
What we were saying when we typed that command was, "append the output from the file home.txt to the file sneakers.txt."
By appending the output, we've saved ourselves a step or two (and a bit of disk clutter) by using existing files, rather than creating a new file.
Compare the results of the files sneakers.txt and saturday now, and you'll see that they're identical. To make your comparison, just type:
cat sneakers.txt; cat saturday |
The contents of both files will be displayed - first sneakers.txt, then saturday (as shown in Figure 13-14).
Don't replace when you append | |
---|---|
Remember that when you append output, you've got to include two greater-than symbols (>>). Otherwise, you'll end up replacing the very file to which you want to append information! |
(By the way, if you're curious about the use of the semi-colon in that last command, read on. We'll cover that later in this chapter.)
Summary | |
---|---|
To append output, use two greater-than symbols (>>). For example: cat addthisfile >> tothisfile. |