---
title: "How to Protect Files Overwriting in UNIX"
description: "Ideas to protect files from over writing in UNIX"
url: https://srinimf.com/2015/09/19/how-to-protect-files-over-written-in-unix/
date: 2015-09-19
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2015/09/protect-files-in-unix.jpg?fit=1200%2C700&ssl=1
categories: ["UNIX"]
tags: ["How to protect files over written in UNIX"]
type: post
lang: en
---

# How to Protect Files Overwriting in UNIX

You can use the “**noclobber**“ shell variable to avoid accidentally overwriting an existing file. It’s a good idea to include this variable in your shell startup file, such as the .cshrc file, as shown here:

![(Linux, UNIX,Ubuntu -JOBS)](https://i0.wp.com/srinimf.com/wp-content/uploads/2015/09/id-10049553.jpg?resize=266%2C400&ssl=1)

## Tips to avoid file Overwriting

## in Unix

```
$set noclobber
```

Related[How to Write Echo Command Correctly in Linux](https://srinimf.com/2022/12/03/how-to-write-echo-command-correctly-in-linux/)

The **noclobber** command is very handy when you’re redirecting output to a file.

Related[How to write COBOL Hello World program in simple way](https://srinimf.com/2016/08/22/how-to-write-cobol-hello-world-program-in-simple-way/)

** Different uses:**

$ echo “Hello, world” >file.txt

$ echo “This will overwrite the first greeting.” >file.txt

$ set -o noclobber

$ echo “Can we overwrite it again?” >file.txt

-bash: file.txt: cannot overwrite existing file

$ echo “But we can use the >| operator to ignore the noclobber.” >|file.txt

$ # Successfully overwrote the contents of file.txt using the >| operator

$ set +o noclobber # Changes setting back

---

**Related Posts**

- [UNIX in 10 Minutes – Programmer’s Friend](https://srinimf.com/2014/04/24/unix-in-10-minutes/)DateApril 24, 2014
- [Unix Scripting: Top Ideas to Write Easily](https://srinimf.com/2017/05/02/unix-script-examples/)DateMay 2, 2017
- [Informatica: Know These 6 Benefits of Unix Shell Scripts](https://srinimf.com/2012/08/27/unix-shell-scripting-why-dwh-professionals-to-learn/)DateAugust 27, 2012

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2015%2F09%2F19%2Fhow-to-protect-files-over-written-in-unix%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2015%2F09%2Fprotect-files-in-unix.jpg%3Ffit%3D1200%252C700%26ssl%3D1&description=How%20to%20Protect%20Files%20Overwriting%20in%20UNIX)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2015/09/19/how-to-protect-files-over-written-in-unix/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2015/09/19/how-to-protect-files-over-written-in-unix/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2015/09/19/how-to-protect-files-over-written-in-unix/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2015/09/19/how-to-protect-files-over-written-in-unix/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Protect%20Files%20Overwriting%20in%20UNIX&body=https%3A%2F%2Fsrinimf.com%2F2015%2F09%2F19%2Fhow-to-protect-files-over-written-in-unix%2F&share=email)
-

### Like this:

Like Loading…
