Solved

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

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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 get maximum transfer speed over LAN 4 105
android secure ftp 3 52
Apache module 5 47
python - find anything after $ question. 9 18
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

830 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