What is the difference between NTFS hard links and directory junctions?

79

33

The title says it all... what's the difference? When do I use one and when do I use the other?

Added: Note that Junction points, Hard links and Symbolic (soft) links are three separate things on NTFS.

Vilx-

Posted 2009-11-09T10:53:24.533

Reputation: 3 293

2

According to Microsoft documentation, there are three distinct categories:
Hard link: http://msdn.microsoft.com/en-us/library/aa365006%28VS.85%29.aspx;
Junction (soft-link): http://msdn.microsoft.com/en-us/library/aa365006%28VS.85%29.aspx;
Symbolic link: http://msdn.microsoft.com/en-us/library/aa363878%28VS.85%29.aspx.

– Josip Medved – 2009-11-09T12:31:37.150

Answers

33

NTFS data files are stored with all their attributes as an "inode" with an ID, and filenames point to the inode. All files have one+ hardlink. some tools can create extra names (hardlinks) which point to the same inode, which has a counter of the number of names pointing to it. If the number goes to zero, the OS deletes the file, the inode.

Special tools, like LN can create additional filenames which point to existing inodes, using a filename that points to them. Note that all the file attributes, including dates and times are stored with the inode, not the filename.

(Extra) hardlinks are useful when you want different names to point to the same data, read or write. They save space and overhead. However, they clobber programs which assume different names point to different data. e.g, if you replace references to duplicate files by hardlinks to a single file, then you have removed any backup you have! The windows OS is one program which assumes different names point to different sets of bits.

Hardlinks have to be on the same physical and logical volume.

They are useful for:

  • reducing path lengths since Windows just supports 256 characters or so.

  • reducing storage space of duplicate data files - eg, you could have multiple backups of a partition with multiple complete directory trees, but with hardlinks for files which have not changed since the previous backup.

Junction points are messier and use Reparse Points (which I do not understand). They can point to different physical or logical volumes but they have to be on the same computer. They apparently can be used like symlinks for directories, allowing a second name for the same destination, but limited to a single computer, rather than just a single volume.

Useful for reducing path lengths, or simplifying working with several programs with hardcoded directory names.

Same caveats, too! Many programs assume different names = different files.

And then there are symbolic links (which can go across networks), shortcuts and PIFs and .....

Also some information from Microsoft itself:

Basically, they're saying to use hard links only for files within the same volume (disk drive letter) and junction points only for directories on the same computer (they can span across different local volumes).

tOM Trottier

Posted 2009-11-09T10:53:24.533

Reputation:

1Does symbolic link work on hard disk (NTFS) level or it's on Windows operating system level concept? – IsmailS – 2010-10-08T05:11:28.237

52

Quick Definitions:

Symbolic link: A link to a file or directory on the same or different volume (drive letter) or even to a remote file or directory (using UNC in its path).

Hard Link: A link to a file on the same volume (drive letter) only. Every file (file's data) has at least 1 hard link (file's name). Deleting all hard links effectively deletes the file.

Junction: A link to a directory on the same or different volume (drive letter) but not to a remote directory.


Details:

Assuming you are working with NTFS volumes in Windows Vista/7 you can make use of the command line tool MKLINK to create all three types of Links:

MKLINK [[/D] | [/H] | [/J]] Link Target

        /D      Creates a directory symbolic link.  Default is 
                a file symbolic link.

        /H      Creates a hard link instead of a symbolic link.

        /J      Creates a Directory Junction.

        Link    specifies the new symbolic link name.

        Target  specifies the path (relative or absolute) that 
                the new link refers to.

Symbolic links: A symbolic link is a file-system object that points to another file system object (eg: file/directory).

  • MKLINK can be used to create symbolic links. Using the /D parameter creates a directory symbolic link. MKLINK does not check if the target is a file or a directory or even if the target exists! This means you can potentially create invalid links like a directory symbolic link to a file or a symbolic link to a no-existing file/directory.

  • Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name while relative links are determined relative to where relative-link specifiers are in a specified path.

  • Absolute symbolic links can point to files/directories on the same or different volume, as well as, to a remote file or directory using the UNC path. However, relative symbolic links are restricted to a single volume.

  • Elevated privileges are required to create a symbolic link though once created elevated privileges are not required to delete the link.

Hard links: A hard link is the file system representation of a file by which more than one path references a single file.

  • MKLINK permits creating hard links (using the /H parameter) only of files (not directories). A hard link can only be created of a file in the same volume.

  • A file with multiple hard links is only actually deleted when all hard links are deleted i.e. the link count reaches zero. So really every file you create has at least one hard link for it whether you use MKLINK or not.

  • Any changes to that file are instantly visible to applications that access it through the hard links that reference it. However, the directory entry size and attribute information is updated only for the link through which the change was made.

  • Note that the attributes on the file are reflected in every hard link to that file, and changes to that file's attributes propagate to all the hard links. For example if you un-set the read-only attribute on a hard link to delete that particular hard link, and there are multiple hard links to the actual file, then you will need to re-set the read-only attribute on the file from one of the remaining hard links to bring the file and all remaining hard links back to the read-only state.

Junctions: A junction (also called a soft link) differs from a hard link in that the storage objects it references are separate directories, and a junction can link directories located on different local volumes on the same computer. Otherwise, junctions operate identically to hard links. Junctions are implemented through reparse points.

  • MKLINK permits creating a junction (using the /J parameter) of a directory (and even of files though this should probably be deemed as an invalid link).

  • A junction could be thought of as the hard link equivalent for a symbolic link to a directory. A junction link cannot be created to a remote directory but can be created to a directory on same/different volume.


References:

[1] Hard Links and Junctions: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365006%28v=vs.85%29.aspx

[2] Creating Symbolic Links: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363878%28v=vs.85%29.aspx

[3] "directory junction" vs "directory symbolic link"?

moo

Posted 2009-11-09T10:53:24.533

Reputation: 905

10

Hard link is just another name for a file. If you have file named A.txt and you have link L.txt, once you delete A.txt, you will still have access to it's data through L.txt. Only when both are deleted file is gone.

On other hand, you have a so called soft-link (junction if it is folder or symbolic link if it is file). In that case, when you delete A.txt, file is really gone. Deleting L.txt has no effect on file what-so-ever.

Hard links can only be used on same partition as originating file and soft-links can be used across partitions.

P.S. File and folder are mostly interchangeable as far as NTFS goes.

Josip Medved

Posted 2009-11-09T10:53:24.533

Reputation: 8 582

3Sorry, junctions and soft links are not the same thing for NTFS. :( – Vilx- – 2009-11-09T12:15:18.283

2http://msdn.microsoft.com/en-us/library/aa365006%28VS.85%29.aspx Quote: "A junction (also called a soft link)" – Josip Medved – 2009-11-09T12:25:52.130

3

However, there is further distinction between junction (aka soft-link) and symbolic link, but I haven't went into details here. For those interested, details can be found in Windows documentation at http://msdn.microsoft.com/en-us/library/aa365006%28VS.85%29.aspx and http://msdn.microsoft.com/en-us/library/aa363878%28VS.85%29.aspx.

– Josip Medved – 2009-11-09T12:28:11.083

1Since Windows Vista, there are both NTFS junctions (can only be pointed to directories) and Unix-style symlinks (can point to literally anything). – user1686 – 2009-11-09T13:01:37.013

0

Hard link: only work for same volume.
Junction: any local volume
Symbolic: local volume or UNC path

| Link Type     | Same Volume | Different Volume | UNC Path |
|---------------|-------------|------------------|----------|
| Hard link     | Yes         | No               | No       |
| Junction      | Yes         | Yes              | No       |
| Symbolic link | Yes         | Yes              | Yes      |

Bonus Chatter

Windows create junctions and symbolic links for AppCompat purposes; for those poorly written applications that think:

  • C:\Users\All Users
  • C:\Users\Default User

are valid.

C:\Users>dir /as

 Directory of C:\Users

04/11/2018  07:45 PM    <SYMLINKD>     All Users [C:\ProgramData]
04/11/2018  07:45 PM    <JUNCTION>     Default User [C:\Users\Default]

Same is true in the user's profile folder, for applications that expect Windows XP era folders to still exist today:

| Folder           | Type       | Target                                                           |
|------------------|------------|------------------------------------------------------------------|    | Application Data | <JUNCTION> | C:\Users\Ian\AppData\Roaming                                     |
| Cookies          | <JUNCTION> | C:\Users\Ian\AppData\Local\Microsoft\Windows\INetCookies         |
| Local Settings   | <JUNCTION> | C:\Users\Ian\AppData\Local                                       |
| My Documents     | <JUNCTION> | C:\Users\Ian\Documents                                           |
| NetHood          | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Network Shortcuts |
| PrintHood        | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Printer Shortcuts |
| Recent           | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Recent            |
| SendTo           | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\SendTo            |
| Start Menu       | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Start Menu        |
| Templates        | <JUNCTION> | C:\Users\Ian\AppData\Roaming\Microsoft\Windows\Templates         |

Ian Boyd

Posted 2009-11-09T10:53:24.533

Reputation: 18 244

2and in gets even crazier on non-english windows installations as you then get EVEN MORE symlinks/junctions, as some may or may not know Windows up to XP actually had local dirnames, which easily can mess up scripts or tutorials made for other Languages. Vista killed that and instead uses localized DISPLAY names, so the actual paths are the same in all languages, but due to then localized windowses have even more links in them than english ones – My1 – 2019-10-16T20:58:52.967

-1

To answer programmatically:

Hard link: shared_ptr<>. Always one exists.

Soft link: weak_ptr<>. Only valid when another hard link exists.

Michael Chourdakis

Posted 2009-11-09T10:53:24.533

Reputation: 15

That's the easy one. :) The question was about Hard Links vs Junctions. ;) – Vilx- – 2016-12-25T22:19:09.053