I\'ve seen somewhere that we can use >> in shell. What\'s the difference between using > and >> in shell?
>>
is for appending whereas >
is for writing (replacing).
There is a difference if the file you're redirecting to already exists:
>
truncates (i.e. replaces) an existing file.
>>
appends to the existing file.
If the file exists, >>
will append to the end of the file, >
will overwrite it.
Both will create it otherwise.
'>>' will let you append data to a file, where '>' will overwrite it. For example:
# cat test
test file
# echo test > test
# cat test
test
# echo file >> test
# cat test
test
file
When you use >, as in:
$ echo "this is a test" > output.txt
The > operator will completely overwrite any contents of the file output.txt if it exists. If the file does not exist, it will be created with the contents "this is a test."
This usage:
$ echo "this is a test" >> output.txt
Will add the link "this is a test" to any content in output.txt (called 'appending'). If the file does not exist, it will be created, and the text will be added.
Adding more knowledge here.
We can also use tee
command to perform the same:
cat newfile | tee filename - rewrites/replaces the file with new content in filename
cat newfile | tee -a filename - appends to the existing content of the file in filename file