The `noclobber` Shield: Preventing Accidental Overwrites
Quick Tip
The `noclobber` Shield: Preventing Accidental Overwrites
Challenge: When redirecting output from commands, you might accidentally overwrite important files with unintended `>` operators if you’re not careful, especially in scripts or when typing quickly.
The Solution: Enable the `noclobber` shell option. This prevents output redirection from overwriting existing files.
set -o noclobber
Why it works: When `noclobber` is set, any attempt to use `>` to redirect output to a file that already exists will result in an error, forcing you to explicitly remove the file or use a different redirection method (like `>>` for appending).
Pro-Tip: To override `noclobber` for a single command when you intentionally want to overwrite a file, use `!>` for redirection, e.g., `command !> existing_file.txt`. You can disable it with `set +o noclobber`.
Linux Tips & Tricks | © ngelinux.com | 6/3/2026
