What's the difference between using > and >> in shell?

后端 未结 6 1933
温柔的废话
温柔的废话 2021-01-14 07:07

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

相关标签:
6条回答
  • 2021-01-14 07:20

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

    0 讨论(0)
  • 2021-01-14 07:24

    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.

    0 讨论(0)
  • 2021-01-14 07:28

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

    Both will create it otherwise.

    0 讨论(0)
  • 2021-01-14 07:30

    '>>' 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
    
    0 讨论(0)
  • 2021-01-14 07:32

    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.

    0 讨论(0)
  • 2021-01-14 07:46

    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
    
    0 讨论(0)
提交回复
热议问题