Sunday, January 14, 2007

Unix 101 - Tip

Here's the first post for the new year.

You have a program that creates a file, writes to it and deletes it later. You want to read that file before its deleted. How do you do that? I have been through such situations in the past, and my crude solution often involved writing a shell script that would keep polling for the existence of the file and then copy it, if it exists. Now here is the Unix way of doing this (I found this in imake faq)

Lets say the name of the transient file is xyz. This is what you do

$ touch fake
$ ln fake xyz

Now run the program that creates xyz. When it tries to remove it, since a hard link exists, it wouldn't be able to do so, and you can access the file afterwards !!

Reminds me of the dead.letter and sendmail exploit. Hard links are fun :))


Sanjay said...

interesting hack :-)

Posy said...

Good post.