In computing, a hard link is a directory entry (in a directory-based file system) that associates a name with a file. All files must have at least one hard link for each file. Creating an additional hard link has the effect of giving one file multiple names (perhaps in different directories too) all of which independently connect to the same data on the disk. This causes an alias effect: a process can open the file by any one of its names and change its content. By contrast, a soft link or “shortcut” to a file is not a direct link to the data itself, but rather a reference to a hard link or another soft link.
Every directory is itself a special file, only it contains a list of file names maintained by the file system. Since directories themselves are files, multiple hard links to directories are possible, which could create a circular directory structure, rather than a branching structure like a tree. For that reason, some file systems forbid the creation of hard links to directories.
POSIX-compliant operating systems, such as Linux, Android, macOS, and the Windows NT family, support multiple hard links to the same file, depending on the file system. For instance, NTFS supports hard links, while FAT and ReFS do not.
Let two hard links, named "LINK A.TXT" and "LINK B.TXT", point to the same physical data. A text editor opens "LINK A.TXT", modifies it and save it. When the editor (or any other app) opens "LINK B.TXT", it can see that changes made to "LINK A.TXT", since both file names point to the same data.
Some editors however break the hard link concept, e.g. emacs. When opening a file for editing, e.g., "LINK B.TXT", emacs renames "LINK B.TXT" to "LINK B.TXT~", loads "LINK B.TXT~" into the editor, and saves the modified contents to a newly created "LINK B.TXT". Now, "LINK A.TXT" and "LINK B.TXT" no longer shares the same data. (This behavior can be changed using the emacs variable
Any number of hard links to the physical data may be created. To access the data, a user only needs to specify the name of any existing link; the operating system will resolve the location of the actual data. Even if the user deletes one of the hard links, the data is still accessible through any other link that remains. Once the user deletes all of the links, if no process has the file open, the operating system frees the disk space that the file once occupied.
Most file systems that support hard links use reference counting. The system stores an integer value with each logical data section that represents the total number of hard links that have been created to point to the data. When a new link is created, this value is increased by one. When a link is removed, the value is decreased by one. When the counter becomes zero, the operating system frees the logical data section. (The OS may not to do so immediately, e.g., when there are outstanding file handles open, for performance reasons, or to enable the undelete command.
This is a simple method for the file system to track the use of a given area of storage, as zero values indicate free space and nonzero values indicate used space. The maintenance of this value guarantees that there will be no dangling hard links pointing nowhere. The data section and the associated inode are preserved as long as a single hard link (directory reference) points to it or any process keeps the associated file open.
To prevent loops in the filesystem, and to keep the interpretation of the "
.." file (parent directory) consistent, operating systems do not allow hard links to directories. UNIX System V allowed them, but only the superuser had permission to make such links. Mac OS X v10.5 (Leopard) and newer use hard links on directories for the Time Machine backup mechanism only.
Hard links can be created to files only on the same volume, i.e., within the same file system. (Different volumes may have different file systems. There is no guarantee that the target volume's file system is compatible with hard linking.)
The maximum number of hard links to a single file is limited by the size of the reference counter. On Unix-like systems the counter is 4,294,967,295 (on 32-bit machines) or 18,446,744,073,709,551,615 (on 64-bit machines.) In some file systems, the number of hard links is limited more strictly by their on-disk format. For example, as of Linux 3.11, the ext4 file system limits the number of hard links on a file to 65,000. Windows limits enforces a limit of 1024 hard links to a file on NTFS volumes.
On Linux Weekly News, Neil Brown criticized hard links as high-maintenance, since they complicate the design of programs that handle directory trees, including archivers and disk usage tools. These apps must take care to de-duplicate files that are linked multiple times in a hierarchy. Brown notes that Plan 9 from Bell Labs, the intended successor to Unix, does not include the concept of a hard link.
Windows NT 3.1 and later support hard links on the NTFS file system. Windows 2000 introduces a
CreateHardLink() function to create hard links, but only for files, not directories. The
DeleteFile() function can remove them.
To create a hard link on Windows, end-users can use:
fsutilutility (introduced in Windows 2000)
mklinkinternal command of Windows Command Prompt (introduced in Windows Vista and Windows Server 2008)
New-Itemcmdlet of PowerShell
To interrogate a file for its hard links, end-users can use:
Get-ChildItemcmdlets of PowerShell. These cmdlets represent each file with an object; PowerShell adds a read-only LinkType property to each of them. This property contains the "
HardLink" string if the associated file has multiple hard links.
The Windows Component Store uses hard links to keep track of different versions of components stored on the hard disk drive.
To interrogate a file for its hard links, end-users can use:
Get-ChildItemcmdlets of PowerShell (see above)
- Symbolic link: Points to a hard link, not the file data itself; hence, it works across volumes and file systems.
- NTFS links: Details the four link types that the NTFS supports—hard links, symbolic links, junction points, and volume mount points
- Shortcut: A small file that points to another in a local or remote location
- freedup – The
freedupcommand frees-up disk space by replacing duplicate data stores with automatically generated hard links
- Pitcher, Lew. "Q & A: The difference between hard and soft links".
- "Link Shell Extension".
- Bach, Maurice J. (1986). The Design of the UNIX Operating System. Prentice Hall. p. 128. ISBN 9780132017992.
- Pond, James (August 31, 2013). "How Time Machine Works its Magic". File System Event Store, Hard Links. Retrieved May 19, 2019.
- "Linux kernel source tree, fs/ext4/ext4.h, line 229".
- "CreateHardLinkA function (winbase.h)". Windows App Development. 13 October 2021 – via Microsoft Docs.
- Brown, Neil (23 November 2010). "Ghosts of Unix past, part 4: High-maintenance designs". Linux Weekly News. Retrieved 20 April 2014.
- "How hard links work". Microsoft Docs.
- "CreateHardLink Function". Windows Development. Microsoft. 10 March 2011. Archived from the original on 2 July 2011 – via MSDN.
Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system, and only for files, not directories.CS1 maint: unfit URL (link)
- "Fsutil hardlink". Windows App Development. Microsoft. 18 April 2012 – via Microsoft Docs.
- "Mklink". Microsoft Docs. Microsoft. 18 April 2012.
- "New-Item (PowerShell 3.0)". Microsoft Docs. Microsoft. 22 June 2020.
If your location is in a FileSystem drive, the following values are allowed: If your location is in a FileSystem drive, the following values are allowed: File[,] Directory[,] Junction[,] HardLink
- "FileSystemProvider.cs". PowerShell / PowerShell repo. Microsoft. 20 November 2021. Lines 8139–8234 – via GitHub.
- "OpenVMS System Manager's Manual, Vol. I" (PDF). VSI. August 2019. Retrieved 2021-01-23.