Protecting files with noclobber
To protect files on Linux by accidently overwritten by > operator, you can use
1 |
set -o noclobber |
Now if you try to overwrite a file with >, you will get error
1 2 3 |
root@ok:~# echo "Hello" > 1.txt -bash: 1.txt: cannot overwrite existing file root@ok:~# |
If you really need to overwrite, use >! operator.
1 |
echo "Hello" >! FILE_NAME |
Example
1 2 3 4 |
root@ok:~# echo "Hello" > 1.txt -bash: 1.txt: cannot overwrite existing file root@ok:~# echo "Hello" >! 1.txt root@ok:~# |
Instead of running the command “set -o noclobber” everytime, you can add it to .bashrc file.