Fact-checked by Grok 2 weeks ago

Hard link

A hard link is a directory entry in a file system that directly references the inode of an existing file, enabling multiple filenames to point to the identical underlying data blocks without duplication. In Unix-like operating systems, every file begins with a single hard link—the original filename—and additional hard links can be created to provide alternative access paths to the same content. Unlike symbolic links, which act as indirect pointers containing a path to another file and can span different file systems or reference non-existent files, hard links are confined to the same file system and always point to extant files, ensuring a direct and atomic association with the data. Hard links are created using the ln command without the -s option, as in ln source_file new_link, which generates a new entry sharing the same inode as the source. This mechanism consumes no additional disk for data storage beyond the original , only requiring for the new entry, and modifications to the through any linked name affect all instances uniformly. The number of hard links to a is visible in the output of ls -l, where it appears in the file permissions line; the and inode are freed only when this count reaches zero, providing inherent protection against accidental deletion as long as at least one link remains. However, hard links cannot reference in standard usage (except by superusers in certain systems, with risks of filesystem loops), and they are limited to regular files, files, or other non- objects within the same filesystem . Hard links promote space efficiency in scenarios like , backups, and , where multiple references to identical are needed without redundancy. They differ fundamentally from file copies, which duplicate and inodes, by maintaining a single physical representation accessible via multiple logical names, thus optimizing storage in inode-based filesystems like those derived from Unix standards. While symbolic links offer greater flexibility for cross-filesystem references and dynamic paths, hard links provide stronger integrity guarantees, as their direct inode binding prevents issues like dangling references if the target moves within the filesystem.

Fundamentals

Definition and Purpose

A hard link is a directory entry in a that associates a directly with the inode of a , enabling multiple directory entries to reference the same underlying data blocks on disk. Unlike a regular entry, which is the initial hard link created upon formation, additional hard links provide alternative names for the identical content without copying the data itself. This mechanism ensures that all hard links to a are equivalent, with modifications through any one affecting the others, as they share the same physical storage location. Central to hard links is the inode, a fundamental in Unix file systems that serves as a for each or within a given . An inode stores essential , including file permissions (such as read, write, and execute for owners, groups, and others), timestamps for last (atime), last modification (mtime), and status change (ctime), and pointers to the data blocks where the file's resides. By pointing to this inode rather than the data blocks directly, hard links allow efficient to the file's and across different directories, while the inode's link count tracks the number of active references to prevent premature data deletion. Full details on inode management are covered in subsequent sections. The primary purpose of hard links is to facilitate space-efficient and management without data duplication, promoting resource conservation in storage-constrained environments. For instance, system administrators often use hard links to maintain a single copy of shared files accessible from multiple locations, such as linking a user's configuration (~/.myprog.conf) to the system-wide setup (/etc/myprog.conf), ensuring consistency while avoiding redundant storage. Additionally, hard links support advanced strategies, such as incremental backups with tools like , where unchanged files from previous snapshots are referenced via hard links to create full-appearing backups that consume minimal additional space—only new or modified data is copied, linking the rest to prior versions. This approach not only saves disk space but also simplifies recovery by preserving a complete view at each backup point.

Role in File Systems

In inode-based file systems like those in Unix, hard links integrate by treating files as independent referenced by multiple entries, rather than as entities contained within a single . Each hard link is a entry that points to the same inode, which stores the file's and blocks, allowing the file to exist under different names without duplicating the underlying . Directories function as indexes or mappings of names to inodes, enabling flexible organization without implying ownership of the file's content. This structure enhances by decoupling persistence from individual names; removing one hard link merely decrements the inode's count without affecting the data, which remains accessible via other links until the count reaches zero. This mechanism ensures that the 's content is preserved as long as at least one exists, providing robustness against accidental deletions of specific names. However, it contrasts with user expectations of "deleting a " as immediately removing its data, since the operation only removes a , potentially leaving the intact elsewhere in the . Hard links enable non-hierarchical views of files by supporting multiple paths to the same inode, transforming the traditional -like representation into a (DAG) for file access. In this model, a single file can be reached from diverse directory branches, facilitating shared access and reducing redundancy, while the overall hierarchy maintains acyclicity to prevent navigation issues. This DAG structure contrasts with a strict , where each file has a unique , and underscores the role of hard links in promoting efficient, multi-parent file relationships within the .

Mechanics

Creation Process

In Unix-like systems, the primary method for creating hard links is the ln command, which by default establishes a hard link between an existing file and a new filename. The basic syntax is ln source target, where source is the path to the existing file and target is the desired name for the new link; for example, ln document.txt document_link.txt creates a hard link named document_link.txt pointing to the same data as document.txt. If the target already exists, the command fails unless the -f (force) flag is used, which removes the existing target before creating the link, as in ln -f source target. At the system level, the ln command invokes the link(2) system call provided by the kernel, which creates a new directory entry for the target path that references the inode of the source file. This process increments the file's hard link count by one in the inode without duplicating the file's data blocks, ensuring both names share the same underlying content, permissions, and ownership. Hard links can only be created within the same filesystem, as the operation relies on direct inode associations. To verify the creation of hard links, the ls -l command displays the link count in its long-format output, shown as a number immediately following the file type and permissions for each entry. For example, after running ln document.txt document_link.txt, the output of ls -l document.txt document_link.txt might appear as:
-rw-r--r-- 2 user group 1024 Nov 10 10:00 document.txt
-rw-r--r-- 2 user group 1024 Nov 10 10:00 document_link.txt
Here, the "2" indicates two hard links to the same inode. A common workflow for managing multiple hard links involves iteratively applying the ln command to create aliases for a single file, such as for versioning or distribution without data duplication; for instance, ln original.txt version1.txt, followed by ln original.txt version2.txt, results in a link count of 3 verifiable via ls -l. Alternatively, to create hard links to multiple source files within a specified directory, use ln file1.txt file2.txt dir/, which creates dir/file1.txt and dir/file2.txt as hard links to their respective sources.

Reference Counting

In Unix-like file systems, reference counting for hard links is managed through an integer field in the inode structure, commonly denoted as i_nlink or i_links_count, which maintains a tally of all directory entries (hard links) pointing to that inode. When a new hard link is created, this count is incremented to reflect the additional reference, ensuring the associated data blocks remain allocated. Conversely, removal of a hard link decrements the count, and the data blocks are freed only if the count reaches zero, thereby preserving file persistence until all links are eliminated. The update process follows a structured algorithm during key system calls, leveraging functions for safe manipulation. For the link() operation, the (VFS) first resolves the target file's inode, then invokes inc_nlink(inode) to atomically increment the link count, marks the inode as dirty for to disk, and adds a new directory entry pointing to the inode. In , this resembles:
function link(oldpath, newpath):
    old_inode = resolve_inode(oldpath)
    if old_inode is [directory](/page/Directory) and not permitted:
        return error
    inc_nlink(old_inode)  // Increment i_nlink atomically
    mark_inode_dirty(old_inode)
    create_directory_entry(newpath, old_inode)
For unlink(), the VFS removes the directory entry, calls drop_nlink(inode) to decrement the count, and checks if it has reached zero; if so, it initiates to free the blocks. for this is:
function unlink(path):
    dir_inode, dentry = resolve_dentry(path)
    inode = dentry.inode
    remove_directory_entry(dentry)
    drop_nlink(inode)  // Decrement i_nlink atomically
    mark_inode_dirty(inode)
    if inode.i_nlink == 0:
        truncate_inode_pages(inode)
        clear_inode(inode)  // Free blocks and inode if needed
Other file operations, such as renaming, may also adjust the count if they effectively create or remove , with the VFS ensuring through locking mechanisms. Edge cases, including power failures during updates, are mitigated by file system technologies like journaling (e.g., in ) or soft updates, which order writes to maintain atomicity and recover consistent link counts post-crash without or leaks. This reference counting mechanism serves as an efficient form of garbage collection in file systems, automatically reclaiming storage only when no hard links remain, which prevents accidental data deletion in scenarios involving multiple references while avoiding the overhead of explicit reference tracking by userspace applications. It ensures that as long as at least one valid link exists, the file's contents persist, supporting use cases like backup versioning or shared data access without duplication.

Comparisons

Symbolic links, also known as soft links, are special files that serve as indirect pointers to another file or by storing a to the target, rather than directly referencing the target's inode as hard links do. This structure allows a symbolic link to function like a containing the target's pathname, which is resolved at runtime during file access. In contrast, hard links create additional entries that point directly to the same inode, ensuring an immediate and unmediated to the file's data. Key behavioral differences arise from this structural variance. Symbolic links can become dangling if the target file is deleted, moved, or renamed, as the stored path no longer resolves to an existing object, whereas hard links remain valid since they share the underlying inode and the file persists until all links are removed. Additionally, symbolic links can span across different s, enabling references between disparate storage volumes, while hard links are confined to the same due to inode locality. Symbolic links also preserve the hierarchical path structure, allowing them to point to directories and maintain relative or absolute paths, a capability hard links lack to avoid cycles and ensure file system integrity. In practice, symbolic links are ideal for creating shortcuts or aliases that provide flexible access without duplicating data, such as in the /etc/alternatives on systems, where they enable dynamic switching between multiple versions of commands or libraries by linking generic names to selected implementations. Hard links, by comparison, support true duplication-free sharing of files within a single , as seen in Unix inode-based designs where multiple names reference the same content to optimize storage and access.

Other Linking Methods

In Windows , junctions serve as a variant of directory linking implemented through reparse points, allowing a directory to point to another directory, potentially across volumes on the same machine. Unlike hard links, which are limited to files on the same volume, junctions enable directory redirection but cannot target files or network locations, and they rely on reparse data stored in the directory to redirect access. Reparse points themselves provide a flexible mechanism for to handle such links by associating user-defined data and tags with files or directories, processed by the filter when accessed, though they introduce limitations like a maximum of 16 KB of data per point and incompatibility with certain attributes. These features combine aspects of hard links' direct referencing with symbolic links' redirection but reduce portability, as they are -specific and may not resolve correctly across different storage configurations. On macOS, aliases function as bundled metadata files that reference other files or directories, storing both the target's pathname and a to enable resolution even if the target moves within the same volume. This approach blends hard link-like persistence with flexibility but adds bloat through embedded , including original details, which can include icons and other attributes not present in standard links. Aliases are confined to HFS and HFS+ file systems, limiting cross-platform and introducing version-specific behaviors, such as the shift in resolution priority from identifiers to pathnames starting in macOS 10.2. In contrast to hard links, aliases do not share the same inode but maintain a separate entity, potentially increasing storage overhead while offering resilience against renames that would break path-based links. In , bind mounts provide a runtime alternative for linking , achieved by remounting a directory subtree at another location using the mount --bind option, effectively creating multiple views of the same content without altering the underlying structure. This serves as a functional equivalent to hard links for , which are generally prohibited to avoid cycles in the file system tree, allowing changes in one to reflect immediately in the other while supporting options like read-only remounts. However, bind mounts operate at the layer rather than the inode level, enabling cross-file-system usage but requiring privileges and explicit unmounting, which contrasts with the static, lightweight nature of hard links and introduces potential administrative overhead in persistent setups.

Limitations

Inode and Cross-Device Constraints

Hard links operate by sharing the same across multiple directory entries, meaning that any modifications to the file's —such as permissions, ownership, or timestamps—affect all linked names simultaneously. The serves as a for the file's data and attributes within its filesystem, ensuring that all hard links reference the identical underlying structure. For instance, altering the read permissions on one hard link will propagate to the others, as there is no independent per link. A significant arises with directories: most systems prohibit creating hard links to directories to prevent the formation of cycles in the filesystem hierarchy, which could lead to infinite loops during traversal or corruption during operations like recursive deletion. This restriction is enforced at the kernel level, returning an EPERM error when attempted, thereby maintaining the acyclic structure essential for reliable path resolution and garbage collection. Hard links are further limited to the same filesystem or mounted volume, as inode numbers are unique only within a single device; attempting to link across boundaries results in an EXDEV error, indicating a cross-device operation not permitted. This design stems from the filesystem's reliance on device-specific inode addressing, preventing ambiguous references between distinct storage volumes. Theoretically, each inode has a bounded capacity for hard links, determined by the filesystem's implementation; for example, the filesystem caps this at 65,000 links per inode to avoid overflow in the link count field. Exceeding this limit during link creation fails with an error, imposing practical restrictions on the degree of file sharing possible within a single filesystem.

Practical Drawbacks

One significant usability challenge with hard links is the absence of a central registry or index to track all links to a given file, making it difficult to identify and manage them without exhaustive filesystem scans. This lack of built-in tracking can complicate , as administrators must rely on verification using inode numbers via commands like ls -i to confirm shared references. Backup tools that do not explicitly support hard links often treat each link as an independent file, leading to duplicated data storage and wasted space on backup media. For instance, preserves hard links only when the --hard-links option is specified; otherwise, it copies the full content for each link, inflating backup sizes unnecessarily. Error-prone scenarios include the accidental deletion of the last hard link to a , which results in irreversible since the underlying inode and content are immediately reclaimed by the filesystem. Additionally, hard links pose challenges in distributed or networked file systems, where their requirement to reside on the same device prevents cross-server linking, limiting their utility in environments like NFS. To mitigate these issues, tools such as find with the -samefile option can discover all hard links by comparing inodes across a directory tree, enabling better inventory management. Best practices include documenting hard link usage in scripts and configuration files to avoid unintended deletions and ensure consistent handling during operations like backups or migrations.

Platform Support

Unix-like Systems

In Unix-like systems, hard links are implemented through an inode-based architecture, where multiple entries can reference the same inode, enabling shared access to . The standard defines the ln utility for creating hard links by default (without the -s option for symbolic links), which invokes the underlying link() to add a new entry for an existing , atomically incrementing its link count. Similarly, the unlink() removes a entry, decrementing the link count, with the freed only when the count reaches zero and no processes hold it open. This mechanism has been a core feature since the early development of Unix in the 1970s, originating in the initial inode design at to support efficient without data duplication. Implementations vary across Unix-like systems, particularly in limits and restrictions. In Linux, the ext4 filesystem enforces a maximum of 65,000 hard links per inode, balancing performance and storage efficiency; exceeding this triggers an EMLINK error via the link() call. BSD-derived systems, such as FreeBSD, prohibit hard links to directories entirely, even for privileged users, to prevent filesystem cycles and maintain a tree-like structure; this exceeds POSIX guidelines, which allow such links only under specific privileged conditions. Hard links integrate seamlessly with standard tools for management and inspection. The stat command reports the current link count using the %h format specifier, providing a quick way to verify the number of references to an inode (e.g., stat -c %h file). In the (FHS), hard links facilitate sharing of essential binaries; for instance, /bin/sh must be a hard or to the POSIX-compliant executable, ensuring consistency across distributions without redundant storage. This usage exemplifies hard links' role in optimizing space for system-critical files in environments.

Windows and Other OSes

Hard links in Windows are supported on the file system, where they were introduced with . Programmatically, they can be created using the CreateHardLink function, which establishes a hard link between an existing file and a new file name on the same volume. This is limited to files and does not support directories, with a maximum of hard links per file (including the original filename) and all links required to reside on the same volume. From the command line, hard links could initially be created using fsutil hardlink create in through XP, while later versions starting with introduced the mklink /H command for this purpose. In macOS, hard links are supported on both the legacy HFS+ and the modern APFS file systems, primarily for files, using the standard Unix ln command, which creates hard links by default. HFS+ additionally allows hard links to directories, a feature utilized in applications like backups, but APFS does not support directory hard links; instead, such cases are converted to symbolic links or aliases during volume conversion. Historically, file systems like and , commonly used for cross-platform compatibility such as USB drives, do not support hard links at all. For Unix-like behavior on Windows, compatibility layers such as provide support for creating hard links via the ln command on volumes, emulating semantics where the underlying file system permits. Similarly, the (WSL) allows Linux distributions to mount volumes and use ln to create hard links, integrating seamlessly with Windows file access. However, in virtualized or shared environments, cross-OS challenges arise, as hard links on may not be traversable or recognized by other operating systems when using non-supporting file systems like or for interchange.

References

  1. [1]
    Hard links and soft links in Linux explained - Red Hat
    Sep 21, 2020 · Every file on the Linux filesystem starts with a single hard link. The link is between the filename and the actual data stored on the filesystem.
  2. [2]
    ln - man pages section 1: User Commands - Oracle Help Center
    A hard link (the default) is a standard directory entry just like the one made when the file was created. Hard links can only be made to existing files. Hard ...Missing: definition | Show results with:definition<|control11|><|separator|>
  3. [3]
    Types of links - IBM
    There are two types of links: hard and symbolic. Hard links ensure file existence, while symbolic links allow access to data in other file systems.Missing: definition | Show results with:definition
  4. [4]
    Hard Link Definition - The Linux Information Project
    Oct 19, 2007 · A hard link is merely an additional name for an existing file on Linux or other Unix-like operating systems.<|control11|><|separator|>
  5. [5]
    Inodes and the Linux filesystem - Red Hat
    Jun 9, 2020 · An inode is an index node. It serves as a unique identifier for a specific piece of metadata on a given filesystem.Missing: authoritative | Show results with:authoritative
  6. [6]
    Inode definition by The Linux Information Project (LINFO)
    ### Summary of Inode Definition and Contents
  7. [7]
    The Ultimate Linux Soft and Hard Link Guide (10 Ln Command ...
    Oct 4, 2010 · Other examples for location dependent configuration files: /var/cluster/members/{memb}/etc/rc.config (network settings etc. etc.) /var ...<|control11|><|separator|>
  8. [8]
    Incremental backups with rsync and hard links | Digitalis Blog
    Nov 13, 2020 · When you create a hard link from one file to another you are creating a separate reference (link) from a new filename to the same inode number.Incremental Backups With... · A Bit Of Background · So What Does This Have To Do...
  9. [9]
    [PDF] The UNIX Time- Sharing System
    If arbitrary links to directories were permitted, it would be quite difficult to detect when the last connection from the root to a directory was severed. 3.3 ...
  10. [10]
    [PDF] Lecture 03: Layering, Naming, and Filesystem Design
    For Unix V6, the file payload is a series of 16-byte slivers that form a table mapping names to inode numbers. Incidentally, you cannot look inside directory ...
  11. [11]
    ln(1) - Linux manual page
    ### Summary of `ln` Manual Page (https://man7.org/linux/man-pages/man1/ln.1.html)
  12. [12]
    link(2) - Linux manual page - man7.org
    link() creates a new link (also known as a hard link) to an existing file. If newpath exists, it will not be overwritten. This new name may be used exactly as ...
  13. [13]
    4.1. Index Nodes — The Linux Kernel documentation
    In a regular UNIX filesystem, the inode stores all the metadata pertaining to the file (time stamps, block maps, extended attributes, etc), not the directory ...
  14. [14]
    File system drivers (Part 2) — The Linux Kernel documentation
    The minix_iget function gets the VFS inode using iget_locked() . If the inode is already existing (not new == the I_NEW flag is not set) the function returns.
  15. [15]
    [PDF] A Solution to the Metadata Update Problem in File Systems
    To be useful for persistent storage, a file system must maintain the integrity of its metadata in the face of unpredictable system crashes, such as power ...
  16. [16]
    symlink
    A symbolic link can cross file system boundaries. Normal permission checks are made on each component of the symbolic link pathname during its resolution.
  17. [17]
    Overview of the Linux Virtual File System — The Linux Kernel documentation
    ### Summary of Symbolic Links and Hard Links in Linux VFS
  18. [18]
    symlink(2) - Linux manual page - man7.org
    Symbolic links are interpreted at run time as if the contents of the link had been substituted into the path being followed to find a file or directory.
  19. [19]
    Symbolic Links (The GNU C Library)
    No readable text found in the HTML.<|control11|><|separator|>
  20. [20]
    /etc - The Linux Documentation Project
    The generic name is not a direct symbolic link to the selected alternative. Instead, it is a symbolic link to a name in the alternatives directory, which in ...
  21. [21]
    Hard Links and Junctions - Win32 apps - Microsoft Learn
    Jul 8, 2025 · Learn how to create and manage hard links and junctions in the NTFS file system ... Junctions are implemented through reparse points. Assuming the ...
  22. [22]
    Reparse Points - Win32 apps - Microsoft Learn
    Jul 9, 2025 · When an application sets a reparse point, it stores this data, plus a reparse tag, which uniquely identifies the data it is storing. When the ...Missing: junctions | Show results with:junctions
  23. [23]
    Aliases and Symbolic Links - Apple Developer
    May 25, 2011 · Aliases and symbolic links are lightweight references to files and folders. Aliases are associated with Mac OS Standard (HFS) and Mac OS ...
  24. [24]
    File System Comparisons - Apple Developer
    May 25, 2011 · File System Comparisons · Aliases and Symbolic Links · Alias Semantics · Symbolic Link Semantics · Avoiding Broken Aliases · Revision History.
  25. [25]
    mount(8) - Linux manual page
    Summary of each segment:
  26. [26]
    inode(7) - Linux manual page - man7.org
    Inode numbers are guaranteed to be unique only within a filesystem (i.e., the same inode numbers may be used by different filesystems, which is the reason that ...Missing: authoritative | Show results with:authoritative
  27. [27]
    link
    The link() function shall create a new link (directory entry) for the existing file, path1. The path1 argument points to a pathname naming an existing file.Missing: device | Show results with:device
  28. [28]
    [PDF] Prism: Lightweight Filesystem Virtualization Via Selective Cloning
    ... hard link /x/y point to the new inode as well. However, given an inode, there is no efficient way to find all hard links pointing to this inode. Prism must ...Missing: registry | Show results with:registry
  29. [29]
    rsync(1) - Linux man page
    Summary of each segment:
  30. [30]
    find(1) - Linux manual page
    ### Summary of `-samefile` Option in `find`
  31. [31]
    ln
    ### Summary of `ln` Utility for Creating Hard Links in POSIX
  32. [32]
    unlink
    ### Summary of unlink() for Removing Hard Links in POSIX
  33. [33]
    ext4(5) - Linux manual page
    ### Summary: Maximum Hard Links in ext4 Filesystem
  34. [34]
  35. [35]
    stat(1) - Linux manual page
    ### Summary: How `stat` Command Shows Link Count in Unix-like Systems
  36. [36]
    [PDF] Filesystem Hierarchy Standard - Linux Foundation
    Mar 19, 2015 · The Filesystem Hierarchy Standard provides requirements and guidelines for file and directory placement under UNIX-like systems to support ...<|control11|><|separator|>