When I edit a file in "vi" editor the inode value of the file is changing. But when edited with cat command the inode value is not changing.
2 Answers
When you want to modify a file, you have two options, each with its benefits and drawbacks.
- You can overwrite the file in place. This does not use any extra space, and conserves the hard links, permissions and any other attribute beyond the content of the existing file. The major drawback of doing this is that if anything happens while the file is being written (the application crashes, or the power goes out), you end up with a partially written file.
- You can write the new version of the file to a new file with a different name, then move it into place. This uses more space and breaks hard links, and if you have write permissions on a file but not on the directory it contains, you can't do it at all. On the flip side, the old version of the file is atomically replaced by the new version, so at every point in time the file name points to a valid, complete version of the file.
Advanced editors such as Vim or Emacs can choose between the two methods. When they use the first method, they normally make a backup file first, which can be recovered if the new file contents cannot be written correctly.
Classical vi overwrites the file in place. So the inode is unchanged.
In Vim, as cjm has already stated, the choice is controlled by the backup
, backupcopy
and writebackup
options.
By default, Vim renames the old file, then writes a new file with the original name, if it thinks it can re-create the original file's attributes. If you want to reuse the existing inode (and so risk losing data, or waste more time making a backup copy), add set backupcopy yes
to your .vimrc
.
-
3+1 For mentioning
writebackup
because I already turned offbackup
andbackupcopy
but inode was still changing. Sep 27, 2019 at 9:53
Most likely, you have set the backup option on, and backupcopy to "no" or "breakhardlink".
-
-
2
-
1@cjm With VIM 7.4, I had no string
backup
in my.vimrc
, but inode was still changing, because the default is to backup current file, deleted afterwards and to /*whenever possible*/ rename the file /*to the backup*/ and write the new one /*under the same name, effectively changing the inode*/. Solved by addingset backupcopy=yes
to.vimrc
. Aug 9, 2017 at 12:28 -
Thanks for pointing to the documentation. I recently discovered that editing a file with vim(which has a soft/hard link) does not change the inode number and your documentation helped me understand why. Nov 8, 2020 at 3:18