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)

Tips to avoid file Overwriting

in Unix

$set noclobber

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

 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

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.