The `noclobber` Shield: Preventing Accidental Overwrites

Quick Tip

The `noclobber` Shield: Preventing Accidental Overwrites

Challenge: You’re working in the terminal and accidentally redirect output to a file that already contains important data, overwriting it. This can happen when using `>` for redirection.

The Solution: Use the `set -o noclobber` shell option. Once enabled, attempting to redirect output to an existing file with `>` will result in an error, preventing accidental data loss.

set -o noclobber

Why it works: `noclobber` is a shell option that modifies the behavior of redirection. It acts as a protective layer, ensuring that you explicitly choose to overwrite a file if that’s your intention (e.g., by using `>|`).

Pro-Tip: To temporarily disable `noclobber` for a single redirection and force an overwrite, you can use `>|`. For example: `command >| existing_file.txt`

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted