Hard link

Hard link

In computing, a hard link is a directory reference, or pointer, to a file on a storage volume. The name associated with the file is simply a label stored in a directory structure that refers the operating system to the file data. As such, more than one name can be associated with the same file. When accessed through different names, any changes made will affect the same file data. Hard links can only refer to data that exists on the same file system.

On POSIX-compliant operating systems, such as all Unix-like systems, hard links are created with the link() system call, or the ln and link command-line utilities.

On Microsoft Windows, hard links can be created only on NTFS volumes. mklink under Vista, fsutil hardlink, and the ln command from Cygwin are examples of tools that can be used to create them.

The process of unlinking disassociates a name from the data on the volume without destroying the associated data. The data is still accessible as long as at least one link that points to it still exists. When the last link is removed, the space is considered free. A process ambiguously called undeleting allows the recreation of links to data that is no longer associated with a name. However, this process is not available on all systems and is often not reliable.

Link counter

Most file systems that support hard links use reference counting. An integer value is stored with each physical data section. This integer represents the total number of 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. If the link count becomes zero, the operating system usually automatically deallocates the data space of the file if no process has the file opened for access. The maintenance of this value assists users in preventing data loss. 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.

On POSIX-compliant operating systems, such as many Unix-variants, 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. In contrast, programming language implementations that use reference counting rarely expose the reference count to the program being executed, since this information is just an implementation detail.


In the figure to the right, two hard links, named "LINK A.TXT" and "LINK B.TXT", point to the same physical data.

If the filename "LINK A.TXT" is opened in an editor, modified and saved, then those changes will be visible even if the filename "LINK B.TXT" is opened for viewing since both filenames point to the same data. The same is true if the file were opened as "LINK B.TXT" - or any other name associated with the data.

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.

If one of the links is removed with the POSIX unlink function (for example, with the UNIX 'rm' command), then the data is 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 files are deleted automatically on program termination, including the case of abnormal termination.

Limitations of hard links

Most modern operating systems don't allow hard links on directories to prevent endless recursion. A notable exception to this is Mac OS X v10.5 (Leopard).

Hard links can only be created to files on the same file system. If a link to a file on a different file system is needed, it may be created with a symbolic link.

See also


Search another word or see hard linkon Dictionary | Thesaurus |Spanish
Copyright © 2014, LLC. All rights reserved.
  • Please Login or Sign Up to use the Recent Searches feature