Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Hard link

From Wikipedia, the free encyclopedia
Directory entry (in a directory-based file system) that associates a name with a file
For the hyperlinking term, seeHardlink (hyperlink).

Incomputing, ahard link is adirectory entry (in adirectory-basedfile system) that associates a name with afile. 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 additionalpaths (i.e., via different names or in different directories).[1] This causes analias effect: a process can open the file by any one of its paths and change its content. By contrast, asoft 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 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 atree. For that reason, some file systems forbid the creation of additional hard links to directories.

POSIX-compliantoperating systems, such asLinux,Android,macOS, and the non-POSIX-compliantWindows NT family,[2] support multiple hard links to the same file, depending on the file system. For instance,NTFS andReFS support hard links,[3] whileFAT does not.

Operation

[edit]
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. Atext 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 asGNU 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 share the same data. (This behavior can be changed using the emacs variablebackup-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

[edit]
Simplified illustration of hard links on typical Unix filesystem. Note that files "A" and "D" both point to same index entry in filesystem'sinode table, making its reference count 2.

Mostfile systems that support hard links usereference counting. The file system stores aninteger value with each logicaldata 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 theundelete 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 associatedinode are preserved as long as a single hard link (directory reference) points to it or any process keeps the associated file open.

OnPOSIX-compliant operating systems, the reference count for a file or directory is returned by thestat() or fstat() system calls in thest_nlink field ofstruct stat.

Limitations

[edit]

To prevent loops in the filesystem, and to keep the interpretation of the ".." file (parent directory) consistent, operating systems do not generally allow hard links to directories.UNIX System V allowed them, but only thesuperuser had permission to make such links.[4]Mac OS X v10.5 (Leopard) and newer use hard links on directories for theTime Machine backup mechanism only.[5]

Hard links can be created to files only on the same volume, i.e., within the same file system. (This is because a hard link is a "name → inode" association where the inode is identified by itsnumber, and inode numbers are only unique within their partition. Also, 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 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&TUnix System 6, released in 1975, the number of hard links allowed was 127.[6][7] 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 ofLinux 3.11, theext4 file system limits the number of hard links on a file to 65,000.[8]Windows enforces a limit of 1024 hard links to a file onNTFS volumes.[9]

OnLinux 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 ahierarchy. Brown notes thatPlan 9 from Bell Labs, the intended successor to Unix, does not include the concept of a hard link.[10]

Platform support

[edit]

Windows NT 3.1 and later support hard links on theNTFS file system.[11] Windows 2000 introduces aCreateHardLink() function to create hard links, but only for files, not directories.[12] TheDeleteFile() function can remove them.

To create a hard link on Windows, end-users can use:

To interrogate a file for its hard links, end-users can use:

  • Thefsutil utility[13]
  • TheGet-Item andGet-ChildItem 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 "HardLink" string if the associated file has multiple hard links.[16]

TheWindows Component Store uses hard links to keep track of different versions of components stored on the hard disk drive.

OnUnix-like systems, thelink()system call can create additional hard links to existing files. To create hard links, end-users can use:

  • Theln utility
  • Thelink utility
  • TheNew-Item cmdlet of PowerShell[15]

To interrogate a file for its hard links, end-users can use:

  • Thestat command
  • Thels -l command
  • TheGet-Item andGet-ChildItem cmdlets of PowerShell (see above)[16]

Unix-like emulation or compatibility software running on Microsoft Windows, such asCygwin andSubsystem for UNIX-based Applications, allow the use of POSIX interfaces.

OpenVMS supports hard links on theODS-5 file system.[17] Unlike Unix, VMS can create hard links to directories.

See also

[edit]
  • 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
    • Alias: macOS implementation of a shortcut
    • Shadow: OS/2 implementation of a shortcut
  • freedup: Thefreedup command frees-up disk space by replacing duplicate data stores with automatically generated hard links
  • rsync: supports--link-dest=DIR so that a new backup folder will use hardlinks of the previous backup if not changed[18] as a space and time saving feature (incremental backup)[19][20][21]

References

[edit]
  1. ^Pitcher, Lew."Q & A: The difference between hard and soft links".
  2. ^"Link Shell Extension".
  3. ^"Resilient File System (ReFS) overview".Microsoft Learn. 26 October 2022 – viaMicrosoft Docs.
  4. ^Bach, Maurice J. (1986).The Design of the UNIX Operating System. Prentice Hall. p. 128.ISBN 9780132017992.
  5. ^Pond, James (August 31, 2013)."How Time Machine Works its Magic". File System Event Store, Hard Links. Archived fromthe original on June 21, 2019. RetrievedMay 19, 2019.
  6. ^"2.2 Error Codes".gnu.org. Free Software Foundation. RetrievedApr 2, 2025.
  7. ^Lions, John (1977)."19.5 link". Sydney Australia: University of New South Wales. line 5917. RetrievedApr 2, 2025.
  8. ^"Linux kernel source tree, fs/ext4/ext4.h, line 229".
  9. ^"CreateHardLinkA function (winbase.h)".Windows App Development. 13 October 2021 – viaMicrosoft Docs.
  10. ^Brown, Neil (23 November 2010)."Ghosts of Unix past, part 4: High-maintenance designs".Linux Weekly News. Retrieved20 April 2014.
  11. ^"How hard links work".Microsoft Docs. 6 January 2011.
  12. ^"CreateHardLink Function".Windows Development.Microsoft. 10 March 2011. Archived from the original on 2 July 2011 – viaMSDN.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.
  13. ^ab"Fsutil hardlink".Windows App Development.Microsoft. 18 April 2012 – via Microsoft Docs.
  14. ^"Mklink".Microsoft Docs.Microsoft. 18 April 2012.
  15. ^ab"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
  16. ^ab"FileSystemProvider.cs".PowerShell / PowerShell repo.Microsoft. 20 November 2021. Lines 8139–8234 – viaGitHub.
  17. ^"OpenVMS System Manager's Manual, Vol. I"(PDF). VSI. August 2019. Retrieved2021-01-23.
  18. ^"rsync(1) - Linux man page".linux.die.net. Archived fromthe original on 2026-01-26. Retrieved2026-01-31.This option behaves like --copy-dest, but unchanged files are hard linked from DIR to the destination directory. The files must be identical in all preserved attributes (e.g. permissions, possibly ownership) in order for the files to be linked together
  19. ^"Rsync's link-dest: Not Great for Deployments - Konstantin Kovshenin". Retrieved2026-01-31.The --link-dest argument allows you to hard-link files from a different destination if they haven't changed, saving both time on transfer, as well as disk space
  20. ^"Incremental backups with rsync and hard links | Digitalis Blog".digitalis.io. Retrieved2026-01-31.If the file in /backup/server1Old is the same as the file on the remote server then instead of copying it over rsync will create a hard link from the file in /backup/server1Old into /backup/server1New
  21. ^Preston, W. Curtis (2007-01-03).Backup & Recovery: Inexpensive Backup Solutions for Open Systems. "O'Reilly Media, Inc.". p. 189.ISBN 978-0-596-55504-7.copying changes into the new directory, and making hard links where possible for unchanged files
Disk and
non-rotating
Optical disc
Flash memory andSSD
host-sidewear leveling
Distributed parallel
NAS
Specialized
Pseudo
Encrypted
Types
Features
Access control
Interfaces
Lists
Layouts
Types
Properties
Organisation
Operations
Linking
Management
Retrieved from "https://en.wikipedia.org/w/index.php?title=Hard_link&oldid=1335833157"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp