tags:

views:

119

answers:

6

I've seen somewhere that we can use >> in shell. What's the difference between using > and >> in shell?

+10  A: 

>> is for appending whereas > is for writing (replacing).

jldupont
What would be an example of >> ?
goe
Yeah, be careful when you use > because it will just overwrite the file completely if it already exists whereas >> will either create a new file if none exists or start appending to the end of the existing one.
Brent Nash
@goe: you would use `>>` to keep adding a new line to the end of a file. For example, a log file.
Agent_9191
echo "more text" >> somefile.txt
jldupont
+1. Yep, and at the risk of over clarifying, `>` will overwrite (remove) anything that's currently in the file.
Dave Paroulek
+3  A: 

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.

Martin B
+3  A: 

If the file exists, >> will append to the end of the file, > will overwrite it.

Both will create it otherwise.

Oded
+1 for being both complete and succinct.
wallyk
+1  A: 

'>>' 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
danben
A: 

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.

Kenny
A: 
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
Venkataramesh Kommoju