Could someone explain to me the difference between >
and >>
when using shell commands?
Example:
ps -aux > log
ps -aux >> log
It seems the result is the same either way.
Could someone explain to me the difference between >
and >>
when using shell commands?
Example:
ps -aux > log
ps -aux >> log
It seems the result is the same either way.
>
is used to overwrite (“clobber”) a file and>>
is used to append to a file.Thus, when you use
ps aux > file
, the output ofps aux
will be written tofile
and if a file namedfile
was already present, its contents will be overwritten.And if you use
ps aux >> file
, the output ofps aux
will be written tofile
and if the file namedfile
was already present, the file will now contain its previous contents and also the contents ofps aux
, written after its older contents offile
.if you write in terminal
It will put the output of
ps aux
to log named file.then if you put
then the next output will be appended below the first. if you put only one
>
it will overwrite the previous file.Yes,
>>
appends,>
always overwrites/destroys the previous content.is the same as
On Wintel it is the same for
.bat
,.cmd
and.ps1
scripts too; common heritage, common sense.Most important difference is that
>
makes shell open a file or file-like object withO_WRONLY|O_CREAT|O_TRUNC
flags - the file will be created or truncated if it exists, while>>
opens file withO_WRONLY|O_CREAT|O_APPEND
flags - file will be created or appended to if it exists. This is evident if you trace system calls, for example withAnd with
Notice that in both cases the file descriptor of the open file is duplicated onto file descriptor 1 ( stdout ) of the command, and that will be inherited by whatever command the shell forks.