The `noclobber` Shield: Prevent Accidental Overwrites
Quick Tip
The `noclobber` Shield: Prevent Accidental Overwrites
Challenge: You’re scripting or working interactively and want to ensure that an output redirection (`>`) doesn’t accidentally overwrite an existing file. This can lead to data loss if you’re not careful.
The Solution: Use the `noclobber` shell option. You can enable it with set -o noclobber.
set -o noclobber
Why it works: When `noclobber` is enabled, attempting to redirect output (`>`) to a file that already exists will result in an error, preventing accidental overwrites. You can still overwrite if you explicitly use `>! `.
Pro-Tip: To disable it and allow overwrites again, use set +o noclobber.
Linux Tips & Tricks | © ngelinux.com | 6/26/2026
