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.
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
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