Solved

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

Posted on 2011-09-21
7
630 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 77

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to mount an LVM partition that is part of a disk image. 3 71
Exchange 2010 unable to mount DB due to corruption 11 110
CentOS 7 Installation 7 72
cannot rename datastore 3 45
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

786 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