Solved

Why does linux change the inode number after editing the file?

Posted on 2011-09-21
7
610 Views
Last Modified: 2012-05-12
Hello, I am wondering why does linux change the inode number after editing the file? if I open a text/conf file in linux and edit it then I find that it has changed the inode number. It does affect my application but it has made me curious.

Please let me know if someone can answer this behavior. Thanks!
0
Comment
Question by:beer9
7 Comments
 
LVL 21

Expert Comment

by:Papertrip
ID: 36577913
Because file size, physical location on disk, ctime/mtime/atime are all stored in the inode, amongst other things of course.
0
 
LVL 76

Accepted Solution

by:
arnold earned 250 total points
ID: 36577916
I might be that you have a mechanism that you are actually editing a copy rather than the real file such that when the transaction is complete, the original is deleted with the temporary copy modified is renamed.

do you have cvs/subversion on the system to keep track of file changes?
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36577939
I wanted to expand on the "other things" from my first answer.  Check out the man page for the stat() function.

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for filesystem I/O */
    blkcnt_t  st_blocks;  /* number of blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 21

Expert Comment

by:Papertrip
ID: 36578003
Found an explanation that is less technical.  I've never seen it written out so nicely :)


The inode contains all the information necessary for a process to access the file, such as file ownership, access rights, file size, time of last access or modification, and the location of the file’s data on disk. Since a file’s data is spread across disk blocks, the inode contains a “table of contents” to help locate this data. Good analogy.

It is important to note the distinction between changing the contents of an inode and changing the contents of a file. The contents of a file only change on a write operation.

The contents of an inode change when

    (1) the contents of the corresponding file change (because when you update the file contents, you change the time of last modification. This is what you can search for with the find command using the -mtime flag, but I digress), or
    (2) when its owner (e.g. using chown), permissions (chmod or chgrp), or any of the other information that is maintained as part of the inode changes.

    Thus, changing the contents of a file automatically implies a change to the inode, whereas a change to the inode does not imply that the contents of the file have changed.
0
 
LVL 12

Assisted Solution

by:hfraser
hfraser earned 250 total points
ID: 36578055
"vi" manages your editting changes by keeping a memory-resident copy of the file along with a ".swp" file that contains all the changes, leaving the original file intact. The combination of the original and the .swp file allow the modified file to be re-created if there are any problems either during the editting session or during the save of the modified file. If the original file were overwritten during the save to preserve the inode, and a problem occured during the save, it would not be possible to re-create the editted file using the original file (now corrupted) plus changes. Instead, a new file is created, and only after that is successul are the original and .swp files deleted.
0
 
LVL 21

Expert Comment

by:Papertrip
ID: 36578078
Apologies I misread your original question and got stuck on my lil tangent.  At least you know all about inodes now though :p

arnold and hfraser are probably on the right track.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36578299
I agree that it's likely the file is being copied.  If you need further assistance, it would help to know which editor you are using.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now