Write to file, but overwrite it if it exists

前端 未结 8 1549
一个人的身影
一个人的身影 2020-12-07 07:29
echo \"text\" >> \'Users/Name/Desktop/TheAccount.txt\'

How do I make it so it creates the file if it doesn\'t exist, but overwrites it if it

相关标签:
8条回答
  • 2020-12-07 08:29

    The >> redirection operator will append lines to the end of the specified file, where-as the single greater than > will empty and overwrite the file.

    echo "text" > 'Users/Name/Desktop/TheAccount.txt'
    
    0 讨论(0)
  • 2020-12-07 08:29

    In Bash, if you have set noclobber a la set -o noclobber, then you use the syntax >|

    For example:

    echo "some text" >| existing_file

    This also works if the file doesn't exist yet


    • Check if noclobber is set with: set -o | grep noclobber

    • For a more detailed explanation on this special type of operator, see this post

    • For a more exhaustive list of redirection operators, refer to this post

    0 讨论(0)
提交回复
热议问题