The `noclobber` Shield: Prevent Accidental Overwrites

Quick Tip

The `noclobber` Shield: Prevent Accidental Overwrites

Challenge: Accidentally overwriting important files when using output redirection (`>`).

The Solution: Use the `set -o noclobber` shell option to prevent output redirection from overwriting existing files.

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, safeguarding your data. To force an overwrite, you can prefix the redirection with `!`, e.g., `!command > existing_file`.

Pro-Tip: You can temporarily disable `noclobber` for a specific redirection by using `command > existing_file` or `command > existing_file 2>&1`.

Linux Tips & Tricks | © ngelinux.com | 7/3/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted