22

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.

18

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

27

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 For mentioning writebackup because I already turned off backup and backupcopy but inode was still changing. – Alireza Mohamadi Sep 27 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.