Operating System - Linux
1829608 Members
1384 Online
109992 Solutions
New Discussion

Edit a file without changing the inode number

 
SOLVED
Go to solution
MathanSankar
New Member

Edit a file without changing the inode number

When u edit a file in unix the inode number changes.
Is there any way to edit a file without changing the inode number???
3 REPLIES 3
Stuart Browne
Honored Contributor
Solution

Re: Edit a file without changing the inode number

Hrm, weird. Never knew that one..

Quick (hack) fix: Make a hard-link to it ('ln '). As the inode reference count will be > 1, it won't destroy the file, and re-create it, it'll use the in-place inode (as it's referenced).

That being said, I guess it depends on the editor you use, or what you're trying to do with the file.

What editor do you use? (I tested with 'vim').
One long-haired git at your service...
MathanSankar
New Member

Re: Edit a file without changing the inode number

Thanks stuart... It works...
Hein van den Heuvel
Honored Contributor

Re: Edit a file without changing the inode number

I think it would depend a bunch on the exact implementation of the editor chosen, and some 'luck' as well.

What Linux? What version? What Editor?

For details on what the editor exactly does use 'strace editor small-file' and study the lines after the exit commands. (Watch in horror all the 'stuff' that is being done before you get control of thr screen! :-)

Here, on my dinky 'thin client' with a Linux 2.4.25 theeditor of choice is vi and is implemented in busybox. The output file is created as:

open("xy", O_WRONLY|O_CREAT|O_TRUNC, 0664) = 3
write(3, "....

So if anything goes wrong, the data is destroyed already.
I could imagine other editors, perhaps doing a create on a new output file first, using a unique temp name, and then when this is succesfully write rename it to the real output, blowing away the original implicitly or explicitly with unlink.


Hope this helps,
Hein van den Heuvel