23

How can I override the file exists: warning from zsh?

> echo > newfile.txt
> echo > newfile.txt  
zsh: file exists: newfile.txt  

In these cases I prefer my shell to not complain and simply overwrite the file, like bash.

Likewise, how to override the following:

$ ls >> /tmp/testfile.txt                                                                                                                                                   
zsh: no such file or directory: /tmp/testfile.txt
  • 2
    set +C should be longer for a comment. But maybe that's because i failed to mention >| for a single-use case. – mikeserv Jun 25 '15 at 14:38
24

Does your setopt output mention noclobber? If so, that's it, just setopt clobber

The documentation for the option is at http://zsh.sourceforge.net/Doc/Release/Options.html#index-file-clobbering_002c-allowing

38

You can force the ZSH to clobber on redirect with:

>!

Same as >, except that the file is truncated to zero length if it exists, even if CLOBBER is unset.

or

>>!

Same as >>, except that the file is created if it does not exist, even if CLOBBER is unset.

14

Others have recommended using >! or >>! but these are not POSIX standard. The POSIx compliant way to overwrite files without prompting is to use >| or >>| and this will work in any shell.

For information see here: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.