Quick Tip
The `set -o noclobber` Shield: Prevent Accidental Overwrites
Challenge: You’re scripting and want to ensure that a redirection operation (`>`) doesn’t accidentally overwrite an existing file, which could lead to data loss.
The Solution: Use the shell option `set -o noclobber` (or `set -C`).
set -o noclobber echo "This will fail if output.txt exists" > output.txt # To override noclobber for a specific redirection, use: echo "This will succeed even if output.txt exists" >| output.txt set +o noclobber # To disable it later
Why it works: When `noclobber` is enabled, attempting to overwrite an existing file with `>` will result in an error instead of the overwrite. The `>|` redirection operator is specifically designed to bypass this protection when needed.
Pro-Tip: You can also set `noclobber` in your shell’s configuration file (e.g., ~/.bashrc) to have it enabled by default for all your interactive sessions.
Linux Tips & Tricks | © ngelinux.com | 6/15/2026
