The `noclobber` Shield: Prevent Accidental Overwrites

Quick Tip

The `noclobber` Shield: Prevent Accidental Overwrites

TITLE: The `noclobber` Shield: Prevent Accidental Overwrites

Challenge: When performing shell operations, especially redirection, it’s easy to accidentally overwrite existing files with unintended output. This can lead to data loss.

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

set -o noclobber

Why it works: When `noclobber` is enabled, attempts to overwrite an existing file using output redirection (`>`) will result in an error instead of performing the overwrite.

Pro-Tip: You can temporarily bypass `noclobber` by using the `| tee` command, which writes to both the file and standard output, or by explicitly using the `>` operator (though this is often discouraged when `noclobber` is active). For example, `echo “force overwrite” >| existing_file.txt` will overwrite even with `noclobber` set.

Linux Tips & Tricks | © ngelinux.com | 6/24/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted