A file on my CentOS does not have write permission and can not be edited using vim. But using echo it can be written, why?
[root@srv chap2]# cat raj.txt
Hello World
[root@srv chap2]# ls -l raj.txt
---------- 1 root root 12 Sep 22 17:37 raj.txt
[root@srv chap2]# echo "Hello World Again" >>...
Shell redirection can edit the file, and so can nano. But emacs and vim cannot. I suspect it has something to do with the latter two editing temp files, while nano actually opens the original (askubuntu.com/a/444079/85695).
First, you get the error because you are using a text editor for this. This means that every time you open the file, edit and save it, the original is overwritten with the new contents. Whether you added a single line to the end or 100 lines all over the place is irrelevant, the point is that the...
I was wrong about vi. Using w! does let me write it. But still, it's weird that vi, nano and emacs behave differently. I would expect the file system permissions to be dealt with at a lower level than whatever application is opening the file.
@terdon I left a comment. Basically, the editors that refuse to write the file for root are implementing a safety check. Root would be able to override it, but it's there anyway. The shell, for obvious reasons, does not do this extra checking.
@Kusalananda Yes. I was just surprised to see high level tools (editors) doing this, I'd assumed there was some issue lower down. But that makes perfect sense, thanks.