Inode number changes for a file in Redhat Linux

Hi,

I have created a file a.txt in Redhat Linux.

Inode number for a file changes every time i update the file using vi editor , gedit etc.

Is there any setting that can be made , such that inode number never changes as that is supposed to be the expected behavior?

Or if we cannot restrict the inode number change , is there a way to identify the file uniquely as inode number does.

I need a way to differentiate between a.txt that is modified and a.txt that was created after renaming the earlier a..txt to a.txt.1

Kindly reply

Thanks in Advance.
Sriram

Hi,

You can look at the Inode detail using the "stat" command as follows;

[root@ekbit13 bin]# stat a
  File: `a'
  Size: 32        	Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2097756     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-07 09:54:27.132865927 +0100
Modify: 2014-09-10 16:24:31.432147640 +0100
Change: 2014-09-10 16:24:31.432147640 +0100

Regards

Dave

Yes I have used the stat command to determine Inode.
The problem is when I modify the file, the inode changes , even though it is supposed to be the same.

bash-2.05$ stat c.txt
  File: `c.txt'
  Size: 92807           Blocks: 192        IO Block: 4096   regular file
Device: ca02h/51714d    Inode: 35225883    Links: 1
Access: (0666/-rw-rw-rw-)  Uid: (559587/    srmm)   Gid: ( 8500/     dba)
Access: 2014-10-08 22:52:27.000000000 -0700
Modify: 2014-10-08 22:34:42.000000000 -0700
Change: 2014-10-08 22:34:42.000000000 -0700
bash-2.05$ gedit c.txt
bash-2.05$ stat c.txt
  File: `c.txt'
  Size: 92813           Blocks: 192        IO Block: 4096   regular file
Device: ca02h/51714d    Inode: 35225885    Links: 1
Access: (0666/-rw-rw-rw-)  Uid: (559587/    srmm)   Gid: ( 8500/     dba)
Access: 2014-10-08 22:53:11.000000000 -0700
Modify: 2014-10-08 22:53:11.000000000 -0700
Change: 2014-10-08 22:53:11.000000000 -0700

If there are multiple links to a file, vi file should not change the i-node number. If there is only one link to a file and you make any changes to the file using vi , it is more efficient to rename the temp file to the final resting place rather than copy the temp file onto the original file and then remove the temp file.

Why do you care if the i-node number changes? Why isn't the file's name ( a.txt ) sufficient for identifying the file?

(Note that ls -i and ls -li will also give you the i-node number even on systems that don't have the stat utility.)

Hey thanks,
I need a way to uniquely determine if a log file is rotated/rolled up or not.
In case it is rotated, the original log file (a.log) will be renamed to a.log.1 and a new a.log is created. So the new a.log will have a different inode no. This way I can determine if a log file has been rotated or not. However ,if the inode no changes for every modification I cannot check if a file is rotated/rolled up or not.
Can you think of some other file attribute that will help me uniquely identify this?
Is there a way to keep the inode constant?

This is just a wild guess - I can not reproduce your findings on my systems and I have no gedit available.
vi usually creates a swap file which you work on instead of the original file. Maybe your vi-implementation replaces the original file with the swap file. vim 7.3 offers the -n flag to disallow the creation of the swap file (:set uc=0 should do the same).

Though this problem reproduces with VI and gedit, this happens when we perform file handling operations using java C and perl. In these cases, the files are modified by the program. Do you think the issue is more related to an OS ?
So say, a java program creates and modifies a file, with each modification the inode changes. This happens not just in my machine but in multiple machines. Can we work around this behavior somehow?

What kind of filesystem do this files reside on?

they are Unix file systems , if that is what you mean?

Hi,

I think what is meant is what is the filesystem type, ext2, ext3, ext4, jfs, zfs, ufs, adfs etc.

Anyway, in answer to your original question, "Why does the Inode number change." It is likely that this is due to the editor, as they quite often work with "shadow copies" of the file you are editing, and the new copy replaces the original file.

Regards

Dave

oh ok. It is UFS

Hi,

I don't use Gedit, however if you open a file in gedit and while it is open have a look in the directory to see if there are other copies of the file as follows;

[root@ekbit13 bin]# ls -l sergen*
-rwxr-xr-x. 1 sc386dm sc386dm 12800 Nov 18  2013 sergen.scr
-rwxr-xr-x. 1 sc386dm sc386dm 13174 Nov  5  2013 sergen.scr~
[root@ekbit13 bin]# stat sergen.scr
  File: `sergen.scr'
  Size: 12800     	Blocks: 32         IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2099809     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-09 10:21:48.695567634 +0100
Modify: 2014-10-09 10:21:48.701567649 +0100
Change: 2014-10-09 10:21:48.721567699 +0100
[root@ekbit13 bin]# stat sergen.scr~
  File: `sergen.scr~'
  Size: 13174     	Blocks: 32         IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2098503     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-07 09:54:27.133865925 +0100
Modify: 2013-11-05 21:46:48.371733236 +0000
Change: 2013-11-05 21:46:48.371733236 +0000
[root@ekbit13 bin]# vim sergen.scr
[root@ekbit13 bin]# stat sergen.scr
  File: `sergen.scr'
  Size: 12800     	Blocks: 32         IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2099808     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-09 10:22:51.895723445 +0100
Modify: 2014-10-09 10:22:51.895723445 +0100
Change: 2014-10-09 10:22:51.919723502 +0100
[root@ekbit13 bin]# vim sergen.scr
[root@ekbit13 bin]# stat sergen.scr~
  File: `sergen.scr~'
  Size: 13174     	Blocks: 32         IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2098503     Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-07 09:54:27.133865925 +0100
Modify: 2013-11-05 21:46:48.371733236 +0000
Change: 2013-11-05 21:46:48.371733236 +0000
[root@ekbit13 bin]# 

As you can see the Inode numbers have changed, below is an actual example using the Bluefish editor.

[~/test] 
(10:34:01)-(sc386dm)-(685)-> touch testfile.txt

[~/test] 
(10:34:14)-(sc386dm)-(686)-> stat testfile.txt
  File: `testfile.txt'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d	Inode: 2098216     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-09 10:34:14.713010608 +0100
Modify: 2014-10-09 10:34:14.713010608 +0100
Change: 2014-10-09 10:34:14.713010608 +0100

[~/test] 
(10:34:22)-(sc386dm)-(687)-> ls -latr
total 12
drwx------. 47 sc386dm sc386dm 4096 Oct  9 10:31 ..
-rw-rw-r--   1 sc386dm sc386dm    0 Oct  9 10:34 testfile.txt~
-rw-rw-r--   1 sc386dm sc386dm    6 Oct  9 10:34 testfile.txt
drwxrwxr-x   2 sc386dm sc386dm 4096 Oct  9 10:34 .

[~/test] 
(10:35:01)-(sc386dm)-(688)-> stat testfile.txt
  File: `testfile.txt'
  Size: 6         	Blocks: 8          IO Block: 4096   regular file
Device: 811h/2065d	Inode: 2099835     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-09 10:34:48.307144361 +0100
Modify: 2014-10-09 10:34:48.307144361 +0100
Change: 2014-10-09 10:34:48.324144444 +0100

[~/test] 
(10:35:13)-(sc386dm)-(689)-> stat testfile.txt~
  File: `testfile.txt~'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 811h/2065d	Inode: 2098216     Links: 1
Access: (0664/-rw-rw-r--)  Uid: (  505/ sc386dm)   Gid: (  500/ sc386dm)
Access: 2014-10-09 10:34:36.018083824 +0100
Modify: 2014-10-09 10:34:14.713010608 +0100
Change: 2014-10-09 10:34:48.324144444 +0100

[~/test] 
(10:35:27)-(sc386dm)-(690)-> 

Hope that this helps.

Regards

Dave

Modifying an existing file does not change the inode number. The code you're using rewrites the file to a new one, then rename()'s the new file with the old name.

How do you stop that? You don't do it.

If your log file inodes are changing, I'd say the problem is, "Who is editing the log files?" There's auditing to figure that out.

It means the file is being replaced by another file, not that the inode on the same file is "changing" somehow. Prevent the directory from being written to by the processes -- to prevent the moving, creation, and deletion of files -- will prevent the inode from "changing" -- by stopping these things from working. The file can still be overwritten.

If you want the inode to remain the same, don't use anything that replaces it. If you use stream commands to alter the file, don't edit "in place" or such -- redirect them to a temp file, then cat the new file over the original. To edit, copy to a temp file, edit the temp file, then cat the temp file over the original. I think this is what commands like crontab -e and visudo do.