views:

715

answers:

3

I was looking for a way to create hard links under Windows and I found this page: http://technet.microsoft.com/en-us/library/cc788097.aspx

To try it out, I created a file (1.txt) on the root of my C: drive with 100 lines of the following content:

C:\1.txt (2.598 bytes):

test test test test test

Then I open the command prompt and type:

fsutil hardlink create C:\2.txt C:\1.txt

Success, 2.txt was created but when I go to see it's size it has exactly 2.598 bytes and also noticed some strange behaviours (as far as my understanding of hard links goes):

  1. If I delete 2.txt (the hard linked file) 1.txt is not deleted, and vice-versa.
  2. If I open 2.txt after I delete 1.txt (the original file) the content is still the same.

How does the fsutil hardlink create command differs from the copy command? And how can I create a true hard link under Windows?

I'm using Windows XP SP 3, and my file system is NTFS.

+3  A: 

That's exactly the behavior that supposed to happen. Are you sure you understand how hard links work? Hard links are really just multiple entries in the file system that refer to the exact same file. If you create a hard link to a file and modify one of the instances of the file, the other file will show up with your changes because they both refer to the same blocks of data on disk.

When you delete one of the hard links, you're really just deleting one of the file system entries referring to that file. The file doesn't get deleted until you delete all of its hard links (including the original one). After you delete 1.txt, 2.txt still exists and refers to the same file that was originally there, only now there is only a single file system entry referring to that file (namely, 2.txt). 2.txt is that file.

Adam Rosenfield
Thank you, I had a different idea in my head.
Alix Axel
A: 

That is a true hard link. In UNIX (I'm not familiar with the Windows variant) a file is just a set of data on the disk (simplistically). It's the directory entry itself that gives that file a name.

When you have two directory entries pointing at the same underlying file, deleting one of them does not delete the file unless it's the last directory entry attached to it (and the file is not still open by a process, but that's a different matter).

paxdiablo
+1  A: 

How does the fsutil hardlink create command differs from the copy command? And how can I create a true hard link under Windows?

After creating the hardlinked file, try editing either. You will discover that the changes are reflected in both files.

Try creating a hard-link of a very large file (say a video or disk-image). Notice that the available space on the disk has not changed (or not changed by more than a few bytes, if additional metadata had to be written to disk to reflect the new dir entry).

TokenMacGuy
Thank you, very nice examples.
Alix Axel
That would depend on the editor, I think. Some rename the old file and create a new file with the original name, thus breaking the link. Unless Windows hard links are different to the UNIX ones, which is entirely possible :-)
paxdiablo
Actually I had no idea ntfs had hardlinks (still not sure I believe it)
TokenMacGuy
NTFS has supported hardlinks and directory "symlinks" called junctions since NT5, and real symlinks since NT6. Really. :)
ephemient
(where NT5 is better known as Windows 2000, and NT6 is better known as Vista or Server 2008)
ephemient