Helping people with computers... one answer at a time.
It's possible to have two different names for the exact same file on disk. The tools, like the concept, are a little obscure but can be very useful.
Can the same file have two different names?
Surprisingly the answer may well be yes. And it's not just different names but potentially different names in different directories that can actually refer to the same file.
Unix and related operating systems have have been able to do this for decades. What most people don't realize is that Windows NT and its successors Windows 2000 and Windows XP also support this; or rather the NTFS filesystem supports this. "This" is the concept of "hard links". To understand what hard links are, we need to take a look at what it means to be a directory entry for a file on disk.
When you open up Windows Explorer to view the contents of a folder or do a "DIR" in a Command shell, you're looking a a list of the files in that folder or a "directory" of those files. The contents of each file live somewhere else on the hard disk; but the file's name, date, and other information is stored in its directory entry.
Perhaps the most important bit of information stored in the directory listing for a file the is location of its contents on the hard disk. The operating system uses the directory information to actually retrieve the contents when you read or modify it.
A common term is that the directory entry "points to" or "links to" the file as illustrated by the diagram below:
So if each file's contents is out on the hard disk and a directory entry merely points to it then what prevents a second directory entry from pointing to the same file contents?
In a word: nothing. At least not under NTFS. You can have many directory entries pointing to the same file contents much like this diagram:
If you change the contents of the file using any of the directory entries that point to it then all of the directory entries will reflect the change.
Creating additional directory entries to point to a file is easy under Windows XP though the command line is a bit long. You start by creating the file however you normally would - that will create the single directory entry and the file contents as illustrated in the first diagram above. To create an additional directory entry that links to the same file in a Command Shell enter:
fsutil hardlink create newname existingfilename
"existingfilename" is the name of the file that already exists on disk. "newname" is the filename to be used in the additional directory entry to be created. So for example, if I have "c:\my documents\latest paper.doc" and I want that same file to also appear elsewhere then I might enter:
fsutil hardlink create "c:\shared files\paper.doc" "c:\my documents\latest paper.doc"
"c:\my documents\latest paper.doc" and "c:\shared files\paper.doc" are now two names for the same file. Change one and it shows up in the other.
Delete one and the other remains. Deleting a directory entry only removes the directory entry. It's only when the last directory entry referencing a file is removed that the file itself is finally removed from the disk.
One last limitation: hard links only work on the same drive. So for example you can't have linked directory entries on D: for files on C:.
Comments on this entry are closed.
If you have a question, start by using the search box up at the top of the page - there's a very good chance that your question has already been answered on Ask Leo!.
If you don't find your answer, head out to http://askleo.com/ask to ask your question.