Site icon New Generation Enterprise Linux

The “No Overwrite” Sentinel: `set -o noclobber`

Quick Tip

The “No Overwrite” Sentinel: `set -o noclobber`

Challenge: You’re working in the terminal and want to redirect output to a file, but you’re concerned about accidentally overwriting an existing file with important data. This is a common pitfall, especially in automated scripts.

The Solution: Utilize the `noclobber` shell option. By setting this option, you prevent the shell from overwriting existing files when using redirection operators like `>`.

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 message, safeguarding your data from accidental deletion. To explicitly overwrite, you’d need to use `>!`.

Pro-Tip: You can check if `noclobber` is currently set by running `set -o | grep noclobber`. To temporarily disable it for a single command, you can prefix the command with `noclobber=0`.

Linux Tips & Tricks | © ngelinux.com | 5/21/2026

0 0 votes
Article Rating
Exit mobile version