387
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.

switz
  • 24,384
  • 25
  • 76
  • 101

9 Answers9

609

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'
Community
  • 1
  • 1
Nylon Smile
  • 8,990
  • 1
  • 25
  • 34
  • 21
    Some environments disallow with something like `-bash: TheAccount.txt: cannot overwrite existing file`. – Tom Russell Mar 27 '17 at 07:37
  • 57
    See answer by @BrDaHa. Use `>|` to force overwrite existing – Jake Jan 16 '18 at 03:27
  • if your environment disallows it. It works on Ubuntu 20.10 with bash 5. – Boris Verkhovskiy Nov 03 '20 at 19:09
  • Semi-related question to this, but what's the best way to pick up all these little nuances about bash? I never knew about this answer but it's hugely helpful. I'm finding it hard to figure out a middle ground between "just absorb the bash reference manual cover to cover" and "googling every problem on Stackoverflow." – Urthor Dec 09 '21 at 00:09
  • 1
    This answer doesn't directly answer the question. As Jake noted, @BrDaHa answer answers it. – macetw Dec 09 '21 at 20:34
153

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

mokagio
  • 16,391
  • 3
  • 51
  • 58
BrDaHa
  • 5,138
  • 5
  • 32
  • 47
  • @BrDaHa How can I also redirect stderr with >| as well? I tried &>| but it didn't work. – Tu Bui Aug 09 '18 at 09:34
  • @TuBui not sure why it's not working for you. On my machine `&>| file` does work. What shell and version are you using? @stimulate for zsh [both should work](http://zsh.sourceforge.net/Doc/Release/Redirection.html), for bash it's [only `>|`](https://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin) – Gerrit-K May 17 '19 at 07:09
  • @Griddo my shell is bash version 4.3.48(1)-release. `echo "aaa" &>| test.txt` results in error `-bash: syntax error near unexpected token |`. My noclobber is set On. – Tu Bui May 17 '19 at 17:46
  • 2
    @TuBui Apparently I wasn't testing correctly. It's not working for me either ... however the documentation doesn't state that it should be possible anyways. You probably have to stick to `echo "aaa" >| test.txt 2>&1` or use another shell. – Gerrit-K May 23 '19 at 06:33
  • `>!` is also working fine for me (see: http://linuxhowtos.org/Tips%20and%20Tricks/Protecting%20files%20with%20noclobber.htm) – CyberMew Mar 20 '20 at 06:42
  • @CyberMew because you’re using zsh – BrDaHa Mar 20 '20 at 07:18
  • @BrDaHa ah yes you are right, it's a no go for bash. – CyberMew Mar 20 '20 at 08:09
40

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...

Community
  • 1
  • 1
Alex Gray
  • 16,007
  • 9
  • 96
  • 118
22

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
computerist
  • 872
  • 8
  • 9
6
#!/bin/bash

cat <<EOF > SampleFile

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

EOF
4

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.

mokagio
  • 16,391
  • 3
  • 51
  • 58
0

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.

Vogon Jeltz
  • 1,166
  • 1
  • 14
  • 31
Narendra Maru
  • 787
  • 7
  • 8
0

If the texts are output of a command, such as ls, you can use cat.

When the noclobber is set:

ls | cat>| TheAccount.txt

Otherwise, you can combine force deletion (whether the file exist or not) and ls command:

file=TheAccount.txt && rm -f $file && ls | cat > $file
Boyi
  • 76
  • 2
  • 3
-4

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).

aaron-coding
  • 2,571
  • 1
  • 23
  • 31