Site icon New Generation Enterprise Linux

The `noclobber` Shield: Preventing Accidental Overwrites

Quick Tip

The `noclobber` Shield: Preventing Accidental Overwrites

TITLE: The `noclobber` Shield: Preventing Accidental Overwrites

Challenge: You’re scripting or working interactively and want to ensure that redirects (`>`, `>>`) don’t accidentally overwrite existing files, preventing data loss or unexpected behavior.

The Solution: Use the `set -o noclobber` shell option.

set -o noclobber

Why it works: When `noclobber` is set, attempting to redirect output to a file that already exists will result in an error instead of overwriting the file. This acts as a safety net for your data.

Pro-Tip: To temporarily bypass `noclobber` for a single redirect, use `&>` or `| tee` instead of `>`. For example, `echo “force overwrite” >| existing_file.txt` or `echo “force overwrite” | tee existing_file.txt`.

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

0 0 votes
Article Rating
Exit mobile version