What is the difference between ">" and ">>"?
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.