Solved

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

Posted on 2011-09-21
7
623 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My bash alias isn't executing 5 49
linux pull logs and export FTP 4 61
gdb doesn't stop on breakpoint 2 51
Linux hostname change 2 56
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

863 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

28 Experts available now in Live!

Get 1:1 Help Now