Ubuntu: What is the difference between “>” and “>>”?



Question:

What is the difference between ls > and ls >>? I need to understand this for my GCSE computing but don't know what the difference is.


Solution:1

> & >> are redirection operators; they transfer output of something, in this case ls, elsewhere. If this output goes to a file, > will truncate the file - ie delete any previous content, whereas >> will append new data onto the end of the file, keeping previous content. This will work with any input, so echo & cat, for example, can also be used this way.

Also of interest is the | operator, which passes the data to another application - so ls | cat -n will give you a line-numbered listing !

Pipes is the relevant term.


Solution:2

The symbols > and >> are used to redirect output to a file.

Both will create the file if file does not exist. If the file already exists, then > will overwrite the file where as >> will append the data to the file.

So ls > myfile will create a document named myfile if it doesn't exist. If myfile is already present and contains some data, then it will be overwritten with the new data you pass it.

Whereas ls >> myfile will create a file if doesn't exist and write data to it. If the file exist with some data, then new data gets added to its end.


Solution:3

IF you use a single >, then it will overwrite the file if it already exists. Be very careful when you use this one.

If you use two >>, then it will just append (in other words, start writing at the bottom of the file) if it already exists. Otherwise, it creates a new file if it doesn't exist.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »