Shell Scripts

This page contains some of the shell scripts and some of the shell utils that are useful in regular work.

How to keep files safe from accidental overwriting with noclobber under BASH shell?

This happens many a times, user may accidentally use '>' operator and override original file. Or sometimes you may use '>' instead of '>>'. In such a cases, user can use noclobber to ask before doing any action.

Set noclobber:

$ set -o noclobber

Now redirect some result to an already existing file called as "temp"

$ cat original > temp

Then bash will give an error message saying:

temp: cannot overwrite exixting file

Rather than setting each time use the below statement to place permenantly the noclobber setting statement as below:

echo "set -o noclobber" >> ~.bashrc

Unset noclobber

set +o noclobber

Temporarily turn off noclobber

ls -l >| temp

The above command will allow user to overwrite the existing "temp" file.
">|" operator is used to force the file to be overwritten.


Page Information

  • 1 year ago [history]
  • View page source
  • You're not logged in
  • No tags yet learn more

Wiki Information

Recent PBwiki Blog Posts