What's is the difference between “>” and “>>” in shell command?












37















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.










share|improve this question




















  • 2





    For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

    – Sergiy Kolodyazhnyy
    Oct 6 '15 at 3:20


















37















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.










share|improve this question




















  • 2





    For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

    – Sergiy Kolodyazhnyy
    Oct 6 '15 at 3:20
















37












37








37


13






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.










share|improve this question
















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.







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 13 '18 at 7:20









Zanna

50.7k13135241




50.7k13135241










asked Nov 27 '13 at 5:35









whale_stewardwhale_steward

63531025




63531025








  • 2





    For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

    – Sergiy Kolodyazhnyy
    Oct 6 '15 at 3:20
















  • 2





    For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

    – Sergiy Kolodyazhnyy
    Oct 6 '15 at 3:20










2




2





For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

– Sergiy Kolodyazhnyy
Oct 6 '15 at 3:20







For additional info on some of the bash operators , refer also to What's the difference between <<, <<< and < < in bash?

– Sergiy Kolodyazhnyy
Oct 6 '15 at 3:20












4 Answers
4






active

oldest

votes


















42














> is used to overwrite (“clobber”) a file and >> is used to append to a file.



Thus, when you use ps aux > file, the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.



And if you use ps aux >> file, the output of ps aux will be written to file and if the file named file was already present, the file will now contain its previous contents and also the contents of ps aux, written after its older contents of file.






share|improve this answer

































    10














    if you write in terminal



    ps aux > log


    It will put the output of ps aux to log named file.



    then if you put



    ps aux >> log


    then the next output will be appended below the first. if you put only one > it will overwrite the previous file.






    share|improve this answer































      4














      Yes, >> appends, > always overwrites/destroys the previous content.



      ps -aux > log


      is the same as



      rm log 2>/dev/null
      ps -aux >> log


      On Wintel it is the same for .bat, .cmd and .ps1 scripts too; common heritage, common sense.






      share|improve this answer

































        3














        Most important difference is that > makes shell open a file or file-like object with O_WRONLY|O_CREAT|O_TRUNC flags - the file will be created or truncated if it exists, while >> opens file with O_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 with



        $ strace -e open,dup2 bash -c 'true >> /dev/null'
        ...
        open("/dev/null", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
        dup2(3, 1) = 1
        dup2(10, 1) = 1


        And with



        $ strace -e open,dup2 bash -c 'true > /dev/null'
        ...
        open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
        dup2(3, 1) = 1
        dup2(10, 1) = 1
        +++ exited with 0 +++


        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.






        share|improve this answer






















          protected by Sergiy Kolodyazhnyy Jan 19 at 23:58



          Thank you for your interest in this question.
          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



          Would you like to answer one of these unanswered questions instead?














          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          42














          > is used to overwrite (“clobber”) a file and >> is used to append to a file.



          Thus, when you use ps aux > file, the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.



          And if you use ps aux >> file, the output of ps aux will be written to file and if the file named file was already present, the file will now contain its previous contents and also the contents of ps aux, written after its older contents of file.






          share|improve this answer






























            42














            > is used to overwrite (“clobber”) a file and >> is used to append to a file.



            Thus, when you use ps aux > file, the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.



            And if you use ps aux >> file, the output of ps aux will be written to file and if the file named file was already present, the file will now contain its previous contents and also the contents of ps aux, written after its older contents of file.






            share|improve this answer




























              42












              42








              42







              > is used to overwrite (“clobber”) a file and >> is used to append to a file.



              Thus, when you use ps aux > file, the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.



              And if you use ps aux >> file, the output of ps aux will be written to file and if the file named file was already present, the file will now contain its previous contents and also the contents of ps aux, written after its older contents of file.






              share|improve this answer















              > is used to overwrite (“clobber”) a file and >> is used to append to a file.



              Thus, when you use ps aux > file, the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.



              And if you use ps aux >> file, the output of ps aux will be written to file and if the file named file was already present, the file will now contain its previous contents and also the contents of ps aux, written after its older contents of file.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 19 at 23:45









              neverMind9

              206112




              206112










              answered Nov 27 '13 at 5:46









              jobinjobin

              19.3k1277109




              19.3k1277109

























                  10














                  if you write in terminal



                  ps aux > log


                  It will put the output of ps aux to log named file.



                  then if you put



                  ps aux >> log


                  then the next output will be appended below the first. if you put only one > it will overwrite the previous file.






                  share|improve this answer




























                    10














                    if you write in terminal



                    ps aux > log


                    It will put the output of ps aux to log named file.



                    then if you put



                    ps aux >> log


                    then the next output will be appended below the first. if you put only one > it will overwrite the previous file.






                    share|improve this answer


























                      10












                      10








                      10







                      if you write in terminal



                      ps aux > log


                      It will put the output of ps aux to log named file.



                      then if you put



                      ps aux >> log


                      then the next output will be appended below the first. if you put only one > it will overwrite the previous file.






                      share|improve this answer













                      if you write in terminal



                      ps aux > log


                      It will put the output of ps aux to log named file.



                      then if you put



                      ps aux >> log


                      then the next output will be appended below the first. if you put only one > it will overwrite the previous file.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 27 '13 at 5:41









                      souravcsouravc

                      26.9k1376105




                      26.9k1376105























                          4














                          Yes, >> appends, > always overwrites/destroys the previous content.



                          ps -aux > log


                          is the same as



                          rm log 2>/dev/null
                          ps -aux >> log


                          On Wintel it is the same for .bat, .cmd and .ps1 scripts too; common heritage, common sense.






                          share|improve this answer






























                            4














                            Yes, >> appends, > always overwrites/destroys the previous content.



                            ps -aux > log


                            is the same as



                            rm log 2>/dev/null
                            ps -aux >> log


                            On Wintel it is the same for .bat, .cmd and .ps1 scripts too; common heritage, common sense.






                            share|improve this answer




























                              4












                              4








                              4







                              Yes, >> appends, > always overwrites/destroys the previous content.



                              ps -aux > log


                              is the same as



                              rm log 2>/dev/null
                              ps -aux >> log


                              On Wintel it is the same for .bat, .cmd and .ps1 scripts too; common heritage, common sense.






                              share|improve this answer















                              Yes, >> appends, > always overwrites/destroys the previous content.



                              ps -aux > log


                              is the same as



                              rm log 2>/dev/null
                              ps -aux >> log


                              On Wintel it is the same for .bat, .cmd and .ps1 scripts too; common heritage, common sense.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Oct 6 '15 at 3:16









                              kos

                              25.6k870121




                              25.6k870121










                              answered Mar 9 '15 at 1:10









                              mckenzmmckenzm

                              30617




                              30617























                                  3














                                  Most important difference is that > makes shell open a file or file-like object with O_WRONLY|O_CREAT|O_TRUNC flags - the file will be created or truncated if it exists, while >> opens file with O_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 with



                                  $ strace -e open,dup2 bash -c 'true >> /dev/null'
                                  ...
                                  open("/dev/null", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
                                  dup2(3, 1) = 1
                                  dup2(10, 1) = 1


                                  And with



                                  $ strace -e open,dup2 bash -c 'true > /dev/null'
                                  ...
                                  open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
                                  dup2(3, 1) = 1
                                  dup2(10, 1) = 1
                                  +++ exited with 0 +++


                                  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.






                                  share|improve this answer




























                                    3














                                    Most important difference is that > makes shell open a file or file-like object with O_WRONLY|O_CREAT|O_TRUNC flags - the file will be created or truncated if it exists, while >> opens file with O_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 with



                                    $ strace -e open,dup2 bash -c 'true >> /dev/null'
                                    ...
                                    open("/dev/null", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
                                    dup2(3, 1) = 1
                                    dup2(10, 1) = 1


                                    And with



                                    $ strace -e open,dup2 bash -c 'true > /dev/null'
                                    ...
                                    open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
                                    dup2(3, 1) = 1
                                    dup2(10, 1) = 1
                                    +++ exited with 0 +++


                                    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.






                                    share|improve this answer


























                                      3












                                      3








                                      3







                                      Most important difference is that > makes shell open a file or file-like object with O_WRONLY|O_CREAT|O_TRUNC flags - the file will be created or truncated if it exists, while >> opens file with O_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 with



                                      $ strace -e open,dup2 bash -c 'true >> /dev/null'
                                      ...
                                      open("/dev/null", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
                                      dup2(3, 1) = 1
                                      dup2(10, 1) = 1


                                      And with



                                      $ strace -e open,dup2 bash -c 'true > /dev/null'
                                      ...
                                      open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
                                      dup2(3, 1) = 1
                                      dup2(10, 1) = 1
                                      +++ exited with 0 +++


                                      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.






                                      share|improve this answer













                                      Most important difference is that > makes shell open a file or file-like object with O_WRONLY|O_CREAT|O_TRUNC flags - the file will be created or truncated if it exists, while >> opens file with O_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 with



                                      $ strace -e open,dup2 bash -c 'true >> /dev/null'
                                      ...
                                      open("/dev/null", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3
                                      dup2(3, 1) = 1
                                      dup2(10, 1) = 1


                                      And with



                                      $ strace -e open,dup2 bash -c 'true > /dev/null'
                                      ...
                                      open("/dev/null", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
                                      dup2(3, 1) = 1
                                      dup2(10, 1) = 1
                                      +++ exited with 0 +++


                                      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.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 20 '18 at 7:03









                                      Sergiy KolodyazhnyySergiy Kolodyazhnyy

                                      71.7k9147313




                                      71.7k9147313

















                                          protected by Sergiy Kolodyazhnyy Jan 19 at 23:58



                                          Thank you for your interest in this question.
                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                          Would you like to answer one of these unanswered questions instead?



                                          Popular posts from this blog

                                          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                          is 'sed' thread safe

                                          How to make a Squid Proxy server?