Comments on: “In-place” editing of files https://backreference.org/2011/01/29/in-place-editing-of-files/ Proudly uncool and out of fashion Sat, 02 May 2015 00:37:33 +0000 hourly 1 https://wordpress.org/?v=5.8.2 By: Cody https://backreference.org/2011/01/29/in-place-editing-of-files/#comment-25196 Sat, 02 May 2015 00:37:33 +0000 http://backreference.org/?p=1127#comment-25196 In reply to waldner.

Actually - yes, I see this. I mentally did a test with sed and saw the inode stay the same. However, while it is true that I did use sed, the time I did check inode is with mv itself (I thought I did both!). Actually, I think I know what I did: I knew it used rename but I did not think of the fact it renames a new file to the old, rather than - as you put it - in-place edit. Which means there is a new inode; there is no other way around it. So seeing mv confirm what I already knew, it worked in my mind even though it wasn't actually 100% correct.

Thanks for clarifying. Truthfully I never thought about the internals of sed in this way because I never had a need to (although I use the option but then I also have backups, and in many files I have not only backups but revision control - but indeed there is that risk). But of course a temporary file does make sense given what you demonstrate (you gave far more examples than I would have thought of, especially if I didn't really think about it for a long while; I'd argue I wouldn't ever think of that many examples). As for rename() and mv - that is another issue entirely, as I do make use of these frequently. Glad I saw this article because there is no such thing as learning too much, as far as I am concerned! Actually, while I'm at it: great site you have here and the \1 at the top for the url is a great addition!

cheers.

]]>
By: waldner https://backreference.org/2011/01/29/in-place-editing-of-files/#comment-25195 Fri, 01 May 2015 23:13:04 +0000 http://backreference.org/?p=1127#comment-25195 In reply to Cody.

There's no denying that rename()/mv preserves the inode. The problem with sed -i is that rename() acts on a different file from the one being edited by sed, so the result ends up having a different inode number:

$ ls -i aaa
109755 aaa
$ sed -i 's/a/b/' aaa
$ ls -i aaa
109759 aaa

If one wants to preserve the inode, one has to do shenanigans like

$ command file > tempfile && cat tempfile > file && rm tempfile
# or
$ cp file tempfile && command tempfile > file && rm tempfile
]]>
By: Cody https://backreference.org/2011/01/29/in-place-editing-of-files/#comment-25194 Fri, 01 May 2015 22:09:25 +0000 http://backreference.org/?p=1127#comment-25194 In reply to Cody.

One instance just came to mind - different file systems. But assuming this isn't the case, I've never seen a different inode for mv (I'm not doubting it is possible, however; I'd gladly be given an example or examples).

]]>
By: Cody https://backreference.org/2011/01/29/in-place-editing-of-files/#comment-25193 Fri, 01 May 2015 21:59:25 +0000 http://backreference.org/?p=1127#comment-25193 I found this by accident but I'm glad I did; this is a really good write up - it covers a lot of issues with in place editing, the subject of temporary files (which some might argue is semantics but that's hardly a concern to me) and risks, as well as bringing other tools/methods up.

But one thing struck me as amiss: the rename system call doesn't change the inode. If it does in some cases it surely does not in all. I've not seen this in practise. I make use of this quite a lot (frequently, and have for many years), actually (not in place editing or anything like that; I refer only to the inode remaining the same). You could write a simple C test to demonstrate this but you could also do as you say:

ls -i file
mv file file2
ls -i file2

You could also see this with GNU sed option (-i) (and indeed it does use a temporary file).

I'd be curious as to what type of environment you're in that you see different inodes from renaming.

]]>
By: Stan https://backreference.org/2011/01/29/in-place-editing-of-files/#comment-25181 Thu, 12 Mar 2015 16:17:24 +0000 http://backreference.org/?p=1127#comment-25181 There are some situations you don't have a choice, for example on a embedded system with limited disk and memory, if you want to do some changes on a (relatively big) file without reflashing the device, then you need real inplace editing, I don't care if the file is inconsistent during the process as if something goes wrong I can reflash anyway.

]]>