Hard link: Difference between revisions
Undid revision 1056335966 by 67.185.21.25 (talk) |
Significant copyediting. I've updated many links. Sometimes, I simplified sentences when the concept was explained in details in an appropriate section. |
||
Line 2: | Line 2: | ||
{{Original research|date=October 2010}} |
{{Original research|date=October 2010}} |
||
In [[computing]], a '''hard link''' is a [[directory entry]] that associates a name with a [[Computer file|file]] |
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]]. 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.<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 names and changes 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, 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 structure|tree]]. For that reason, some file systems forbid the creation of hard links to directories. |
||
Creating an additional hard link has the effect of giving one file multiple names (e.g. different names in different directories) all of which independently connect to the same data on the disk, none of which depends on any of the others.<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]]: e.g. if the file is opened by any one of its names, and changes are made to its content, then these changes will also be visible when the file is opened by an alternative name. By contrast, a [[soft link]] or [[File shortcut|“shortcut”]] to a file is not a direct link to the data itself, but rather is a short file that contains the text of a [[Filename|file name]], or a location that gives direct access to yet another file name within some directory. The name contained in or referred to by the [[soft link]] may either be a hard link or another soft link. This also creates aliasing, but in a different way. |
|||
⚫ | [[POSIX]]-compliant [[operating system]]s, such as [[Linux]], [[Android (operating system)|Android]], [[macOS]], and the [[Windows NT|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]] supports hard links, while [[File Allocation Table|FAT]] and [[ReFS]] do not. |
||
Every [[Directory (computing)|directory]] is itself a file, only special because it contains a list of file names maintained by the [[file system]]. |
|||
⚫ | |||
==Operation== |
|||
⚫ | |||
⚫ | |||
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. |
|||
Support also depends on the type of file system being used. For instance, the [[NTFS]] file system supports multiple hard links, while [[File Allocation Table|FAT]] and [[ReFS]] do not. |
|||
⚫ | 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 <code>backup-by-copying</code>.) |
||
==Usage== |
|||
⚫ | On |
||
⚫ | 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. |
||
⚫ | [[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}}</ref> However, only since Windows XP there is a <code>CreateHardLink()</code> function to create hard links by giving a new filename to the [[Master File Table]] entry.<ref>{{Cite web|date=13 October 2021|title=CreateHardLinkA function (winbase.h)|url=https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createhardlinka|website=Windows App Development|publisher=[[Microsoft]]|via=[[Microsoft Docs]]|quote=Establishes a hard link between an existing file and a new file. (...) The winbase.h header defines CreateHardLink as an alias which automatically selects the ANSI or Unicode version of this function}}</ref> The <code>DeleteFile()</code> function can remove them. |
||
=== Reference counting === |
|||
⚫ | |||
⚫ | |||
⚫ | * The <code>fsutil</code> utility (introduced in [[Windows 2000]])<ref>{{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 [[Windows PowerShell]]<ref>{{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> |
||
⚫ | Most [[file system]]s 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. |
|||
Unix-like emulation or compatibility software running on Windows, such as [[Cygwin]] and [[Subsystem for UNIX-based Applications]], allow the use of POSIX interfaces on Windows. |
|||
⚫ | |||
⚫ | [[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. |
||
== |
== Limitations == |
||
⚫ | To prevent loops in the filesystem, and to keep the interpretation of the "{{code|..}}" 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.<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}}</ref> |
||
⚫ | Most [[file system]]s that support hard links use [[reference counting]]. |
||
Hard links can be created to files only on the same volume, i.e., within the same file system. (Different volume may have different file systems. There is no guarantee that the target volume's file system is compatible with hard linking.) |
|||
The maintenance of this value guarantees that there will be no dangling hard links pointing nowhere (which can and does happen with symbolic links) and that filesystem file and associated [[inode]] will be preserved as long as a single hard link (directory reference) points to it or any process keeps the associated file open, relieving the burden of this accounting from programmer or user. 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 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.<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 were criticized 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> |
||
==Example== |
|||
⚫ | |||
In the figure to the right, two hard links, named "LINK A.TXT" and "LINK B.TXT", point to the same physical data. |
|||
==Platform support== |
|||
⚫ | |||
⚫ | On [[Unix-like]] systems, additional hard links to existing files are created with the {{Code|link()}} [[system call]], or the [[ln (Unix)|ln]] and [[Link (Unix)|link]] command-line utilities. The <code>[[stat (Unix)|stat]]</code> command can reveal how many hard links point to a given file. 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. |
||
⚫ | [[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}}</ref> However, only since Windows XP there is a <code>CreateHardLink()</code> function to create hard links by giving a new filename to the [[Master File Table]] entry.<ref>{{Cite web|date=13 October 2021|title=CreateHardLinkA function (winbase.h)|url=https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createhardlinka|website=Windows App Development|publisher=[[Microsoft]]|via=[[Microsoft Docs]]|quote=Establishes a hard link between an existing file and a new file. (...) The winbase.h header defines CreateHardLink as an alias which automatically selects the ANSI or Unicode version of this function}}</ref> The <code>DeleteFile()</code> function can remove them. |
||
If the file "LINK A.TXT" is opened in an editor, modified and saved, then those changes will be visible if the file "LINK B.TXT" is then opened for viewing since both filenames point to the same data ("opened", because, on [[POSIX]] systems, an associated [[file descriptor]] remains valid after opening, even when the original file is moved). The same is true if the file were opened as "LINK B.TXT" — or any other name associated with the data. |
|||
⚫ | |||
⚫ | Some editors however break the hard link concept, e.g. [[emacs]]. When opening a file "LINK B.TXT" |
||
⚫ | * The <code>fsutil</code> utility (introduced in [[Windows 2000]])<ref>{{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 [[Windows PowerShell]]<ref>{{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> |
||
⚫ | |||
⚫ | |||
⚫ | [[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. |
||
If one of the links is removed with the [[POSIX]] unlink function (for example, with the UNIX <code>rm</code> command), then the data are still accessible through any other link that remains. If all of the links are removed and no process has the file open, then the space occupied by the data is freed, allowing it to be reused in the future. This semantic allows for deleting open files without affecting the process that uses them. This technique is commonly used to ensure that [[temporary file]]s are deleted automatically on program termination, including the case of abnormal termination. |
|||
==Limitations of hard links== |
|||
⚫ | To prevent loops in the filesystem, and to keep the interpretation of {{code|..}} (parent directory) consistent, |
||
Hard links can be created to files only on the same volume. If a link to a file on a different volume is needed, it may be created with a [[symbolic link]]. |
|||
⚫ | 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 |
||
⚫ | |||
==See also== |
==See also== |
Revision as of 19:46, 21 November 2021
![]() | This article possibly contains original research. (October 2010) |
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.[1] This causes an alias effect: a process can open the file by any one of its names and changes 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,[2] 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.
Operation
![](http://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Hard_Link_Illustration.svg/220px-Hard_Link_Illustration.svg.png)
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 backup-by-copying
.)
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
![](http://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Simplified_illustration_of_hard_links_on_typical_UN%2AX_filesystem.png/220px-Simplified_illustration_of_hard_links_on_typical_UN%2AX_filesystem.png)
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.
On POSIX-compliant operating systems, the reference count for a file or directory is returned by the stat() or fstat() system calls in the st_nlink
field of struct stat
.
Limitations
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.[3] Mac OS X v10.5 (Leopard) and newer use hard links on directories for the Time Machine backup mechanism only.[4]
Hard links can be created to files only on the same volume, i.e., within the same file system. (Different volume 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.[5] Windows limits enforces a limit of 1024 hard links to a file on NTFS volumes.[6]
On Linux Weekly News, Neil Brown criticized hard links were criticized 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.[7]
Platform support
On Unix-like systems, additional hard links to existing files are created with the link()
system call, or the ln and link command-line utilities. The stat
command can reveal how many hard links point to a given file. 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.
Windows NT 3.1 and later support hard links on the NTFS file system.[8] However, only since Windows XP there is a CreateHardLink()
function to create hard links by giving a new filename to the Master File Table entry.[9] The DeleteFile()
function can remove them.
To create a hard link on Windows, an end-user can use:
- The
fsutil
utility (introduced in Windows 2000)[10] - The
mklink
internal command of Windows Command Prompt (introduced in Windows Vista and Windows Server 2008)[11] - The
New-Item
cmdlet of Windows PowerShell[12]
The Windows Component Store uses hard links to keep track of different versions of components stored on the hard disk drive.
OpenVMS supports hard links on the ODS-5 file system.[13] Unlike Unix, VMS can create hard links to directories.
See also
- Fat link
- Symbolic link or soft link, which unlike hard link, only provides the text of an “actual” file name, not file data itself.
- NTFS symbolic link – the NTFS implementation.
- NTFS junction point – similar to an NTFS directory symbolic link, but not the same.
- alias (Mac OS) – a method for linking files introduced in classic Mac OS System 7, and still available in macOS, which is in some ways similar to a symbolic link. Note that true symbolic links are also available in macOS.
- shadow (OS/2) – the OS/2 implementation
- ln (Unix) – The
ln
command, which is used to create new links on Unix-like systems. - freedup – The
freedup
command frees-up disk space by replacing duplicate data stores with automatically generated hard links
References
- ^ 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.
- ^ "CreateHardLinkA function (winbase.h)". Windows App Development. Microsoft. 13 October 2021 – via Microsoft Docs.
Establishes a hard link between an existing file and a new file. (...) The winbase.h header defines CreateHardLink as an alias which automatically selects the ANSI or Unicode version of this function
- ^ "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
- ^ "OpenVMS System Manager's Manual, Vol. I" (PDF). VSI. August 2019. Retrieved 2021-01-23.