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