Appending Output
Appending adds lines to the end of a file. The old lines stay, and the new lines join them.
Add lines to the end
>> adds each line after the last one. cat shows all three lines in order.
$ cd ~/practice
$ echo one > count.txt
$ echo two >> count.txt
$ echo three >> count.txt
$ cat count.txt> wipes the old lines
A fresh > erases the file first. Only the new line stays.
$ cd ~/practice
$ echo one > count.txt
$ echo two >> count.txt
$ echo fresh > count.txt
$ cat count.txt>> makes the file too
>> starts a new file when none exists yet. Three appends give three lines, and wc -l counts them.
$ cd ~/practice
$ rm -f count.txt
$ echo one >> count.txt
$ echo two >> count.txt
$ echo three >> count.txt
$ wc -l count.txtExample
$ cd ~/practice
$ echo morning > day.txt
$ echo noon >> day.txt
$ echo night >> day.txt
$ cat day.txtKey points
- Use >> to add standard output to the end of a file.
- >> keeps the old lines and adds new ones after them.
- > erases the file first and writes fresh lines.
- Count the lines in a file with wc -l.
In this example, echo writes morning to day.txt. Two more echo lines join it with >>. cat shows all three lines in order.
Note
>> creates the file if it is not there yet.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does the double greater-than sign do?