Using the noclobber
option:
noclobber
is a shell option in Unix-like systems that prevents accidental overwriting of existing files when using output redirection with the >
operator. This option is supported only in interactive shells, however. Keep in mind that your scripts will still overwrite files even with noclobber
enabled.
To enable the noclobber
option, use the set
command:
Add this line to your .bashrc
or .zshrc
for permanent effect. Zsh uses setopt
and unsetopt
, but it also supports set -o
and set +o
.
If you want the correct zsh option, add this instead:
Example of noclobber
preventing file overwriting
Output redirection allows you to save the output of a command to a file. However, using the >
operator can accidentally overwrite existing files. With noclobber
enabled, this won’t happen:
The >|
operator
You can still overwrite the file while the noclobber
option is enabled. You just have to use the >|
operator.
Example:
Disabling noclobber
Bash: