Hard link
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, a hard link is a directory entry that associates a name with a file
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

 on a file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...

. (A directory is itself a special kind of file that contains a list of such entries). The term is used in file systems which allow multiple hard links to be created for the same file. This has the effect of creating multiple names for the same file, causing an aliasing
Aliasing (computing)
In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated to all aliased names, which may not be expected by the programmer...

 effect: e.g. if the file is opened by 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 on such file systems is not a link to a file itself, but to a file name. This also creates aliasing, but in a different way.

Directories are files, so multiple hard links to directories are possible; however, their unrestricted creation is usually avoided, because of the cyclic structures this may create.

Hard links—that is, multiple directory entries to the same file—are supported by POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

-compliant and partially POSIX-compliant operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s, such as GNU/Linux, Android, Apple's Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

 (see Time Machine
Time Machine (Apple software)
Time Machine is a backup utility developed by Apple. It is included with Mac OS X and was introduced with the 10.5 "Leopard" release of Mac OS X. The software is designed to work with the Time Capsule as well as other internal or external drives.-Overview:...

), Windows NT4 and later Windows NT
Windows NT
Windows NT is a family of operating systems produced by Microsoft, the first version of which was released in July 1993. It was a powerful high-level-language-based, processor-independent, multiprocessing, multiuser operating system with features comparable to Unix. It was intended to complement...

 operating systems.

Support also depends on the type of file system being used. For instance, the NTFS
NTFS
NTFS is the standard file system of Windows NT, including its later versions Windows 2000, Windows XP, Windows Server 2003, Windows Server 2008, Windows Vista, and Windows 7....

 file system supports hard links, while FAT
File Allocation Table
File Allocation Table is a computer file system architecture now widely used on many computer systems and most memory cards, such as those used with digital cameras. FAT file systems are commonly found on floppy disks, flash memory cards, digital cameras, and many other portable devices because of...

 does not.

Usage

On POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

-compliant and partially POSIX-compliant operating systems, such as all Unix-like
Unix-like
A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....

 systems, additional hard links to existing files are created with the link system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...

, or the ln and link
Ln (Unix)
ln is a standard Unix command used to create links to files.- Link files :Links allow more than one file name to refer to the same file, elsewhere.There are two types of links, both of which are created by ln:...

 command-line utilities. The stat
Stat (Unix)
stat is a Unix system call that returns useful data about a file inode. The semantics of stat vary between operating systems. As an example, the Unix command ls uses it to retrieve information on : time of last modification , time of last status change and time of last access .-stat functions and...

command can reveal how many hard links point to a given file. The link count is also included in the output of ls -l.

On Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

, hard links can be created using the mklink /H command on Windows NT 6.0
Windows NT 6.0
Windows NT 6.0 can refer to these releases of Microsoft Windows:*Windows Vista*Windows Server 2008*Windows Small Business Server 2008...

 and later systems (such as Windows Vista
Windows Vista
Windows Vista is an operating system released in several variations developed by Microsoft for use on personal computers, including home and business desktops, laptops, tablet PCs, and media center PCs...

), and in earlier systems (Windows 2000, XP) using fsutil hardlink create.
The Windows API
Windows API
The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces available in the Microsoft Windows operating systems. It was formerly called the Win32 API; however, the name "Windows API" more accurately reflects its roots in 16-bit Windows and its support on...

 from Windows 2000 onwards includes a CreateHardLink call to create hard links, DeleteFile is used to remove them, and GetFileInformationByHandle can be used to determine the number of hard links associated with a file. Hard links require an NTFS
NTFS
NTFS is the standard file system of Windows NT, including its later versions Windows 2000, Windows XP, Windows Server 2003, Windows Server 2008, Windows Vista, and Windows 7....

 partition. Unix-like emulation or compatibility software running on Windows, such as Cygwin
Cygwin
Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin provides native integration of Windows-based applications, data, and other system resources with applications, software tools, and data of the Unix-like environment...

 and Subsystem for UNIX-based Applications, allow the use of POSIX interfaces under Windows.

The process of unlinking dissociates a name from the data on the volume without destroying the associated data. The data are 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 are no longer associated with a name. However, this process is not available on all systems and is often not reliable.

Link counter

Most file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...

s that support hard links use reference counting
Reference counting
In computer science, reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource...

. An integer
Integer
The integers are formed by the natural numbers together with the negatives of the non-zero natural numbers .They are known as Positive and Negative Integers respectively...

 value is stored with each physical data
Data
The term data refers to qualitative or quantitative attributes of a variable or set of variables. Data are typically the results of measurements and can be the basis of graphs, images, or observations of a set of variables. Data are often viewed as the lowest level of abstraction from which...

 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
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

-compliant operating systems, such as many Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

-variants, the reference count for a file or directory is returned by the stat
Stat (Unix)
stat is a Unix system call that returns useful data about a file inode. The semantics of stat vary between operating systems. As an example, the Unix command ls uses it to retrieve information on : time of last modification , time of last status change and time of last access .-stat functions and...

 or fstat system calls in the st_nlink field of struct stat.

Example

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 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
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 systems, an associated file descriptor
File descriptor
In computer programming, a file descriptor is an abstract indicator for accessing a file. The term is generally used in POSIX operating systems...

 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
Emacs
Emacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...

. When opening a file "LINK B.TXT" for editing, emacs first 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". Using this approach, the two hard links are now "LINK A.TXT" and "LINK B.TXT~" (the backup file); "LINK B.TXT" would now have just one link and no longer shares the same data as "LINK A.TXT". (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.

If one of the links is removed with the POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 unlink function (for example, with the UNIX rm 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
Temporary file
Temporary files may be created by computer programs for a variety of purposes; principally when a program cannot allocate enough memory for its tasks, when the program is working on data bigger than the architecture's address space, or as a primitive form of inter-process communication.- Auxiliary...

s are deleted automatically on program termination, including the case of abnormal termination.

Limitations of hard links

To prevent endless recursion, most modern operating systems don't allow hard links on directories. In addition, hard links on directories would lead to inconsistency on parent directory
Parent directory
In computing, the parent directory of a given directory A is the directory B in which A is located. In As absolute path, B is the predecessor of A....

 entries. A notable exception to this is Mac OS X v10.5
Mac OS X v10.5
Mac OS X Leopard is the sixth major release of Mac OS X, Apple's desktop and server operating system for Macintosh computers. Leopard was released on 26 October 2007 as the successor of Tiger , and is available in two variants: a desktop version suitable for personal computers, and a...

 (Leopard) and newer, which use hard links on directories for the Time Machine
Time Machine (Apple software)
Time Machine is a backup utility developed by Apple. It is included with Mac OS X and was introduced with the 10.5 "Leopard" release of Mac OS X. The software is designed to work with the Time Capsule as well as other internal or external drives.-Overview:...

 backup mechanism only. Symbolic link
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

s and NTFS junction point
NTFS junction point
An NTFS junction point is a feature of the NTFS file system that provides the ability to create a symbolic link to a directory which then functions as an alias of that directory...

s are generally used instead for this purpose.

Hard links can only be created to files on the same volume. If a link to a file on a different volume is needed, it may be created with a symbolic link
Symbolic link
In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

.

The maximum number of hard links to a single file is limited by the size of the reference counter: with NTFS this is limited to 1023 because a 10 bit field is used for this purpose. On Unix-like systems the counter is usually machine-word-sized (32 or 64-bit: 4294967295 or 18446744073709551615 links, respectively).

See also

  • Symbolic link
    Symbolic link
    In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

  • NTFS junction point
    NTFS junction point
    An NTFS junction point is a feature of the NTFS file system that provides the ability to create a symbolic link to a directory which then functions as an alias of that directory...

  • alias (Mac OS)
    Alias (Mac OS)
    In Mac OS System 7 and later, an alias is a small file that represents another object in a local, remote, or removable file system and provides a dynamic link to it; the target object may be moved, and the alias will still link to it...

  • shadow (OS/2)
    Shadow (OS/2)
    In the graphical Workplace Shell of the OS/2 operating system, a shadow is an object that represents another object.A shadow as a stand-in for any other object on the desktop, such as a document, an application, a folder, a hard disk, a network share or removable medium, or a printer. A target...

  • ln (Unix)
    Ln (Unix)
    ln is a standard Unix command used to create links to files.- Link files :Links allow more than one file name to refer to the same file, elsewhere.There are two types of links, both of which are created by ln:...

    —The ln command, which is used to create new links on Unix-like systems.
  • freedup
    Freedup
    freedup is a program to scan directories or file lists for duplicate files. The file lists may be provided to an input pipe or internally generated using find with provided options. There are more options to specify the search conditions more detailed. Other options influence the performed actions,...

    —The freedup command frees-up disk space by replacing duplicate data stores with automatically generated hard links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK