33

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 2

42

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.

1
  • 3
    +1 For mentioning writebackup because I already turned off backup and backupcopy but inode was still changing. Sep 27, 2019 at 9:53
23

Most likely, you have set the backup option on, and backupcopy to "no" or "breakhardlink".

4

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .