Ask Leo!

Can the same file have two different names?

Home » Windows » Windows Oddities

Summary: 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:.

Article 137 | Posted January 9, 2004

Recent Comments
0 Comments

Is this possible on Win 2000 ? I tried but fsutil seems to be not there.

Posted by: Sanjeev at September 21, 2004 2:25 AM

Yes and no. The operating system and NTFS supports it. However there are no command-line tools that come with Win2k.

Posted by: Leo at September 21, 2004 8:31 PM

There is a utility in 2003 Resouce Kit: Hlscan.exe

Posted by: Anonymous at October 13, 2004 9:38 PM

hmm.. I got a problem similar to what's mentioned above. I was deleting duplicate files from my Ipod and I came across a music file that exists in folder "." which is a subfolder of "Enigma" This folder (directory) doesn't seem to exist when I try to navigate to it via windows explorer, but through itunes I can reach the file AND it shows up as a legitimate sub-directory and file. I thought it was some weird windows glitch that dupicated the directory, and I tried to delete it. Thats when I discovered that this sub-directory and it's files actually point back to the parent directory. Deleting sub-directory "." deletes the original files. I'm perplexed on how to solve this so that directory "." no longer exists. I've read the info on HLscan.exe seems only to list the files. Anyone know how to delete the "other" files names

Posted by: omar at February 13, 2005 4:54 AM

Is there any way to do this for directories, instead of files? E.g. I want "C:\My Documents" to be a hard link to "C:\Documents and Settings\Kevin J. Wangler\My Documents". I tried using the same command and it just said 'Error: Access is denied'. So, I assume this isn't possible, but I thought I'd ask. Thanks!

Posted by: Kevin J. Wangler at April 4, 2005 7:36 PM

To answer my own question: Yes, it can be done! I just found a freeware tool called Winbolic Link which allows you to create symlinks (symbolic links), much like in *NIX. It's available at http://www.pearlmagik.com/winbolic/ (I'm not affiliated with PearlMagik or Winbolic Link; it's just something I found doing a Google search. I just tried it and created a C:\My Documents symlink, and it worked great.)

Posted by: Kevin J. Wangler at April 4, 2005 7:53 PM

Is there a SAFE (or 'least dangerous') way to DEFRAGMENT a hard drive on which I have created a great deal of NTFS hard links? I understand that Windows Explorer does not 'know' whether it is looking at a 'hard link' of a file or not, and hence I am worried that the intricate system of hard links that I have built up within my hard drive will be damaged or lost completely when attempting to defragment the drive.

Alternatively, is there a way to 'back up' the NTFS master file table in such a way that if the hard link structure is damaged or destroyed by defragmenting the drive, that the back-up version of that original (pre-defragmentation) file table can be 'restored' somehow ?

I wish I could afford to simply back up the entire drive contents prior to defragmenting it (just in case of such an unwanted degradation of the hard-link structure), but unfortunately I have approximately a terabyte of information stored on my internal hard drives, and I literally cannot afford another terabyte of storage at this time !

Posted by: Steve Brownstein at April 8, 2006 9:23 PM

Defragging shouldn't affect hardlinks.

Posted by: Leo at April 8, 2006 9:49 PM

I am trying to create a batch file that will rename all dvr-ms files to mpg in a specific directory on a daily basis, instead of using the copy command and doubling the used disk space per file (about 4gb), I am trying to use the --fsutil hardlink create-- command to create a duplicate filename just with a different extension in the same directory. HOWEVER... when I use the "*." identifier to perform this action, it tells me that I cant do that. This is the string that I am entering: "fsutil hardlink create d:\record~1\*.mpg(new) d:\record~1\*.dvr-ms(old)" what am I doing wrong?

Posted by: Jon at November 19, 2006 12:34 PM

You'll need to look into the FOR statement and execute the command one file at a time.

A simple example of FOR:

FOR %F IN (*.MPG) DO echo %F

I think you should be able to construct a FOR that does what you want. It'll take some experimentation, so do back up your files :-).

Posted by: Leo Notenboom at November 19, 2006 8:42 PM

Post a comment on "Can the same file have two different names?":






(Email Address will not be published.)

Remember Me?

By popular demand...
my tip jar
Cuppa Joe
Buy Leo a Latte!

(you may use HTML tags for style)

New!

RSS feed Subscribe to the RSS Feed specifically for comments on this article.

Before commenting, please...

Please wait. Your comment is being processed ...


Ask Your Question:


ask-leo.com
Web

Stay Informed

Weekly Newsletter

Archives

By Category
By Date

Advertisers

Advertise on Ask Leo!

««   »»

Question? - Ask Leo!
Who is Leo?
Link to Leo!

Terms, Conditions & Privacy