3.2 Appending: “>>”

Append STDOUT or STDERR to an existing file

Keep in mind that using the operator “>” always overwrites the destination file, meaning that if we redirect STDOUT to an already existing file, the content of this file will be overwritten without warning!

If we want to continue appending the output to an existing file, we then have to use the operator “>>” (two >).

Appending STDOUT to a file

echo "Line 1" > out.txt; cat out.txt
#Line 1
echo "Line 2" > out.txt; cat out.txt
#Line 2
echo "Line 3" >> out.txt; cat out.txt
#Line 2
#Line 3

Once we finish appending STDOUT to a file, that file will contain all previous outputs as shown in the example above.

We can also append STDERR and STDOUT to an existing file using “>>” and the same operator as before “2>&1”

echo "Line 1" > out.txt
ls out.txt other.txt >> out.txt 2>&1
cat out.txt
#Line 1
#ls: cannot access other.txt: No such file or directory out.txt