Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Hard link
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Directory entry (in a directory-based file system) that associates a name with a file}} {{for|the hyperlinking term|Hardlink (hyperlink)}} In [[computing]], a '''hard link''' is a [[directory entry]] (in a [[Directory (computing)|directory]]-based [[file system]]) that associates a name with a [[Computer file|file]]. Thus, each file must have at least one hard link. Creating additional hard links for a file makes the contents of that file accessible via additional [[Path (computing)|paths]] (i.e., via different names or in different directories).<ref>{{cite web|last1=Pitcher|first1=Lew|title=Q & A: The difference between hard and soft links|url=http://linuxgazette.net/105/pitcher.html}}</ref> This causes an [[Aliasing (computing)|alias effect]]: a process can open the file by any one of its paths and change its content. By contrast, a [[soft link]] or [[File shortcut|“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 on many systems, containing a list of file names instead of other data. Hence, multiple hard links to directories are possible, which could create a circular directory structure, rather than a branching structure like a [[Tree structure|tree]]. For that reason, some file systems forbid the creation of additional hard links to directories. [[POSIX]]-compliant [[operating system]]s, such as [[Linux]], [[Android (operating system)|Android]], [[macOS]], and the non POSIX compliant [[Windows NT family]],<ref>{{Cite web|url=http://schinagl.priv.at/nt/hardlinkshellext/hardlinkshellext.html|title=Link Shell Extension}}</ref> support multiple hard links to the same file, depending on the file system. For instance, [[NTFS]] and [[ReFS]] support hard links,<ref>{{cite web |title=Resilient File System (ReFS) overview |url=https://learn.microsoft.com/en-us/windows-server/storage/refs/refs-overview#the-following-features-are-available-with-refs-and-ntfs |website=Microsoft Learn |via=[[Microsoft Docs]] |date=26 October 2022}}</ref> while [[File Allocation Table|FAT]] does not. ==Operation== [[File:Hard Link Illustration.svg|right|thumb|An illustration of the concept of hard linking]] 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 saves it. When the editor (or any other app) opens "LINK B.TXT", it can see those changes made to "LINK A.TXT", since both file names point to the same data. So from a user's point of view this is one file with several filenames. Editing any filename modifies "all" files, however deleting "any" filename except the last one keeps the file around. However, some editors, such as [[GNU Emacs]], break the hard link concept. 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 <code>backup-by-copying</code>.) 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. === Reference counting === [[File:Simplified illustration of hard links on typical UN*X filesystem.png|thumb|Simplified illustration of hard links on typical Unix filesystem. Note that files "A" and "D" both point to same index entry in filesystem's [[inode]] table, making its reference count 2.]] Most [[file system]]s that support hard links use [[reference counting]]. The file 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. On [[POSIX]]-compliant operating systems, the reference count for a file or directory is returned by the [[Stat (Unix)|stat()]] or fstat() system calls in the <code>st_nlink</code> field of <code>struct stat</code>. == Limitations == To prevent loops in the filesystem, and to keep the interpretation of the "{{code|..}}" file (parent directory) consistent, operating systems do not generally allow hard links to directories. [[UNIX System V]] allowed them, but only the [[superuser]] had permission to make such links.<ref name="bach">{{cite book |first=Maurice J. |last=Bach |year=1986 |title=The Design of the UNIX Operating System |url=https://archive.org/details/designofunixoper00bach/page/128 |url-access=registration |publisher=Prentice Hall |page=128 |isbn=9780132017992 }}</ref> [[Mac OS X v10.5|Mac OS X v10.5 (Leopard)]] and newer use hard links on directories for the [[Time Machine (macOS)|Time Machine]] backup mechanism only.<ref name="PondHwTimeMachineWorks">{{cite web | last1=Pond | first1=James | url=http://baligu.com/pondini/TM/Works.html | title=How Time Machine Works its Magic | access-date=May 19, 2019 | date=August 31, 2013 | at=File System Event Store, Hard Links | archive-date=June 21, 2019 | archive-url=https://web.archive.org/web/20190621092705/https://www.baligu.com/pondini/TM/Works.html | url-status=dead }}</ref> 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 on a particular type of file system is limited by the size of the that type of file system's reference counter and the size of the copy of the reference counter in the operating system's in-memory per-file data structure; it may also be limited by a policy choice in the operating system code. Exceeding the permitted number of links results in an error. In AT&T [[Version 6 Unix|Unix System 6]], released in 1975, the number of hard links allowed was 127.<ref>{{cite web |author=<!-- not stated --> |title=2.2 Error Codes |url=https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html |website=gnu.org |publisher=Free Software Foundation |access-date=Apr 2, 2025}}</ref><ref>{{cite web |last=Lions | first=John |title=19.5 link | at=line 5917| date=1977 | url=https://warsus.github.io/lions-/ |location=Sydney Australia |publisher=University of New South Wales |access-date=Apr 2, 2025}} </ref> On Unix-like systems the in-memory 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.<ref>{{cite web | url = https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/ext4/ext4.h?id=6e4664525b1db28f8c4e1130957f70a94c19213e#n229 | title = Linux kernel source tree, fs/ext4/ext4.h, line 229}}</ref> [[Windows]] limits enforces a limit of 1024 hard links to a file on [[NTFS]] volumes.<ref>{{cite web |title=CreateHardLinkA function (winbase.h) |url=https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createhardlinka |website=Windows App Development |via=[[Microsoft Docs]] |date=13 October 2021}}</ref> 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.<ref>{{cite web |url=https://lwn.net/Articles/416494/ |title=Ghosts of Unix past, part 4: High-maintenance designs |website=Linux Weekly News |date=23 November 2010 |access-date=20 April 2014 |first=Neil |last=Brown}}</ref> ==Platform support== [[Windows NT 3.1]] and later support hard links on the [[NTFS]] file system.<ref>{{Cite web|url=https://docs.microsoft.com/en-us/archive/blogs/joscon/how-hard-links-work|title=How hard links work|website=Microsoft Docs|date=6 January 2011 }}</ref> Windows 2000 introduces a <code>CreateHardLink()</code> function to create hard links, but only for files, not directories.<ref>{{Cite web|date=10 March 2011|title=CreateHardLink Function|url=http://msdn.microsoft.com/en-us/library/Aa363860|url-status=usurped<!--I'm using an older edition of the source here because Microsoft once had a tradition of striking the names of unsupported products from its documentation.-->|archive-url=https://web.archive.org/web/20110702043053/http://msdn.microsoft.com/en-us/library/Aa363860|archive-date=2 July 2011|website=Windows Development|publisher=[[Microsoft]]|via=[[MSDN]]|quote=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.}}</ref> The <code>DeleteFile()</code> function can remove them. To create a hard link on Windows, end-users can use: * The <code>fsutil</code> utility (introduced in [[Windows 2000]])<ref name=":0">{{cite web|date=18 April 2012|title=Fsutil hardlink|url=https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-r2-and-2008/cc788097(v=ws.10)|website=Windows App Development|publisher=[[Microsoft]]|via=Microsoft Docs}}</ref> * The <code>mklink</code> internal command of [[Windows Command Prompt]] (introduced in [[Windows Vista]] and [[Windows Server 2008]])<ref>{{cite web |title=Mklink |url=https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc753194(v=ws.10) |website=Microsoft Docs |publisher=[[Microsoft]] |date=18 April 2012}}</ref> * The <code>New-Item</code> cmdlet of [[PowerShell]]<ref name=":1">{{cite web |title=New-Item (PowerShell 3.0) |url=https://docs.microsoft.com/en-us/previous-versions/powershell/module/microsoft.powershell.management/new-item?view=powershell-3.0 |website=Microsoft Docs |publisher=[[Microsoft]] |date=22 June 2020 |quote=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}}</ref> To interrogate a file for its hard links, end-users can use: * The <code>fsutil</code> utility<ref name=":0" /> * The <code>Get-Item</code> and <code>Get-ChildItem</code> cmdlets 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 {{Sic|"<code>Hard|Link</code>"|hide=y}} string if the associated file has multiple hard links.<ref name=":2">{{Cite web|date=20 November 2021|title=FileSystemProvider.cs|url=https://github.com/PowerShell/PowerShell/blob/88ba3f61fa4f8388cb98533563d190d8fcb6c55b/src/System.Management.Automation/namespaces/FileSystemProvider.cs#L8139-L8234|website=PowerShell / PowerShell repo|publisher=[[Microsoft]]|at=Lines 8139–8234|via=[[GitHub]]}}</ref> The [[WinSxS|Windows Component Store]] uses hard links to keep track of different versions of components stored on the hard disk drive. On [[Unix-like]] systems, the {{Code|link()}} [[system call]] can create additional hard links to existing files. To create hard links, end-users can use: * The [[ln (Unix)|ln]] utility * The [[Link (Unix)|link]] utility * The <code>New-Item</code> cmdlet of PowerShell<ref name=":1" /> To interrogate a file for its hard links, end-users can use: * The <code>[[stat (Unix)|stat]]</code> command * The <code>[[ls]] -l</code> command * The <code>Get-Item</code> and <code>Get-ChildItem</code> cmdlets of PowerShell (see above)<ref name=":2" /> Unix-like emulation or compatibility software running on Microsoft Windows, such as [[Cygwin]] and [[Subsystem for UNIX-based Applications]], allow the use of POSIX interfaces. [[OpenVMS]] supports hard links on the [[Files-11|ODS-5]] file system.<ref>{{cite web|url=https://vmssoftware.com/docs/VSI_SYS_MGMT_MANUAL_VOL_I.PDF|title=OpenVMS System Manager's Manual, Vol. I|access-date=2021-01-23|publisher=VSI|date=August 2019}}</ref> Unlike Unix, VMS can create hard links to directories. ==See also== * [[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 (computing)|Shortcut]]: A small file that points to another in a local or remote location **[[alias (Mac OS)|Alias]]: macOS implementation of a shortcut ** [[shadow (OS/2)|Shadow]]: OS/2 implementation of a shortcut * [[freedup]] – The <code>freedup</code> command frees-up disk space by replacing duplicate data stores with automatically generated hard links == References == {{Reflist}} {{File systems}} {{Computer files}} [[Category:Computer file systems]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:Computer files
(
edit
)
Template:File systems
(
edit
)
Template:For
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)
Template:Sic
(
edit
)