HowTo Keep Files Safe From Accidental Overwriting With noclobber BASH Shell Option

The general format for redirecting output on Bash/ksh shell is:


command > output.txt
command > /dev/device

However, you may end up overwriting file accidentally using > operator. For example:

$ ls -l *.c > output.txt

If file output.txt exists and is a regular file it will be overwritten. Say as a root user you typed “command > /etc/passwd” instead of “command < /etc/passwd". This can spell disaster for /etc/passwd file. On one occasion I had used > when I meant to use >> (append) operator.

How do I avoid accidental overwriting of a file on bash shell?

You can tell bash shell not to delete file data / contents by mistake by setting noclobber variable. It can keep you from accidentally destroying your existing files by redirecting input over an already-existing file.

How do I set noclobber option to prevent overwriting files on bash shell?

Open the Terminal and type the following command:
$ set -o noclobber
Create a test file:
$ echo "foo bar"> output.txt
Next, try to write to a file called output.txt:
$ cat > output.txt
ls -l > output.txt
Sample outputs:

bash: output.txt: cannot overwrite existing file

Add set -o noclobber to your ~/.bashrc file:
$ echo 'set -o noclobber' >> ~/.bashrc

How do I turn off noclobber on bash shell?

Type the following command:
$ set +o noclobber

Using + rather than - causes these flags to be turned off.

How do I temporary turn off noclobber on bash shell?

Sometime you just need to turn off noclobber for single operation. Use >| operator to force the file to be overwritten:
$ ls /etc >| output.txt
$ less output.txt

Putting it all together

Here is a quick demo about avoiding unintentional clobbering (overwrting) a file on bash and ksh shell:

Animated gif 01:  Bash / ksh prevent unintentional clobbering / overwriting a file

Animated gif 01: Bash / ksh prevent unintentional clobbering / overwriting a file

Recommended readings
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

4 comments… add one
  • Artem Nosulchik Sep 11, 2007 @ 8:34

    There is also chattr utility that is very useful to prevent files being overwritten and/or modified even under root. In order to protect critically important file just execute chattr +i /path/to/file. To disable protection execute chattr -i /path/to/file.

  • V.Balaviswanathan May 12, 2009 @ 8:47


    The utiltiy chattr is not only used to prevent overwritten but also can make a file undeletable

    To view a file which has this attribute set we use the command


    I hope you know this command but to refresh i gave this command

    Thanks and Regards


  • Andy Apr 28, 2013 @ 20:26

    I know this an ancient post, but the command under “Add set -o noclobber to your ~.bashrc file” has two problems: The ‘smart’ quotes need replace by standard quotes, and the ~ should have a / after it.

    • 🐧 nixCraft Oct 18, 2013 @ 6:51

      The post has been updated to include your response. I appreciate your post and feedback!

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.