Write to file, but overwrite it if it exists

BashUnix

Bash Problem Overview


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 already exists. Right now this script just appends.

Bash Solutions


Solution 1 - Bash

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'

Solution 2 - Bash

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

Solution 3 - Bash

Despite NylonSmile's answer, which is "sort of" correct.. I was unable to overwrite files, in this manner..

echo "i know about Pipes, girlfriend" > thatAnswer > zsh: file exists: thatAnswer

to solve my issues.. I had to use... >!, á la..

[[ $FORCE_IT == 'YES' ]] && echo "$@" >! "$X" || echo "$@" > "$X"

Obviously, be careful with this...

Solution 4 - Bash

If your environment doesn't allow overwriting with >, use pipe | and tee instead as follows:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt'

Note this will also print to the stdout. In case this is unwanted, you can redirect the output to /dev/null as follows:

echo "text" | tee 'Users/Name/Desktop/TheAccount.txt' > /dev/null

Solution 5 - Bash

#!/bin/bash

cat <<EOF > SampleFile

Put Some text here 
Put some text here
Put some text here

EOF

Solution 6 - Bash

Just noting that if you wish to redirect both stderr and stdout to a file while you have noclobber set (i.e. set -o noclobber), you can use the code:

cmd >| file.txt 2>&1

More information about this can be seen at https://stackoverflow.com/a/876242.

Also this answer's @TuBui's question on the answer @BrDaHa provided above at Aug 9 '18 at 9:34.

Solution 7 - Bash

To overwrite one file's content to another file you use the single greater than sign, using two will append.

echo  "this is foo" > foobar.txt
cat foobar.txt
    > this is foo

echo "this is bar" > foobar.txt
cat foobar.txt
    > this is bar
    

echo "this is foo, again" >> foobar.txt
cat foobar.txt
    > this is bar
    > this is foo, again
    

As mentioned in other answers, if you have noclobber set then use the >| operator.

Solution 8 - Bash

If you have output that can have errors, you may want to use an ampersand and a greater than, as follows:

my_task &> 'Users/Name/Desktop/task_output.log' this will redirect both stderr and stdout to the log file (instead of stdout only).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionswitzView Question on Stackoverflow
Solution 1 - BashNylon SmileView Answer on Stackoverflow
Solution 2 - BashBrDaHaView Answer on Stackoverflow
Solution 3 - BashAlex GrayView Answer on Stackoverflow
Solution 4 - BashcomputeristView Answer on Stackoverflow
Solution 5 - BashBalaji Boggaram RamanarayanView Answer on Stackoverflow
Solution 6 - BashgreensaxmanView Answer on Stackoverflow
Solution 7 - BashNarendra MaruView Answer on Stackoverflow
Solution 8 - Bashaaron-codingView Answer on Stackoverflow