Linux下错误代码E212: Can’t open file for writing

错误:
Linux下错误代码E212: Can’t open file for writing
产生原因(一):
一般都是因为一次创建了文件夹同时创建了文件导致,vi /etc/test/test.txt
解决方法:
手动 先进入etc目录下:cd /etc/
手动创建文件夹docker:mkdir test/
进入docker:cd test/
创建demo.json文件并进入编辑:vi test.txt
按 i 进入编辑模式
编辑完后,按ESC退出编辑模式,然后保存并退出: :wq
产生原因(二):
用户权限不够,普通用户vi进行不了保存,需要使用超级用户才行
解决办法:
切换为超级用户再进行编辑
根据以上问题延伸
在使用vim 对文件或配置进行编辑的时候,在保存时发现当前用户没有写权限。又不想放弃当前编辑的内容,怎么办呢?
解决办法:
引用stackoverflow
“For some reason the file you are writing to cannot be created or overwritten.
The reason could be that you do not have permission to write in the directory
or the file name is not valid.
Vim has a builtin help system, I just quoted what it says to :h E212. You might want to edit the file as a superuser like sudo vim FILE. Or if you don’t want to leave your existing vim session (and know have proper sudo rights), you can issue:
:w !sudo tee % > /dev/null
Which will save the file. ”
这样文件的所有者还是root,依然没有写的权限,现在只要更改文件的所有者和组
1 | sudo chown 用户名:组名 文件名 |
当你用上述方法强制写的时候,会提示如下:
1 | #1) Respect the privacy of others. |