Hard Link Definition

A hard link is merely an additional name for an existing file on Linux or other Unix-like operating systems.

Any number of hard links, and thus any number of names, can be created for any file. Hard links can also be created to other hard links. However, they cannot be created for directories, and they cannot cross filesystem boundaries or span across partitions.

The operating system makes no distinction between the name that was originally assigned to a file when it was first created and any hard links that are subsequently created to that file other than that they are merely multiple names for the same file. This is because the original name and any hard links all point to the same inode. An inode is a data structure (i.e., an optimized way of storing information) that stores all the information about a file (e.g., its size, its access permissions, when it was created and where it is located on the system) except its name(s) and its actual data. The fact that inode numbers are unique only within any filesystem is the reason that they do not work across filesystems and partitions.

Hard links are created with the ln command. For example, the following would create a hard link named hlink1 to a file named file1, both in the current directory (i.e., the directory in which the user is currently working):

ln file1 hlink1

When a hard link is created, there is no obvious indication that it is any different from any other file. That is, hard links appear to be files of the same type as their target files (i.e., the files to which they are linked) when they are viewed with commands such as ls (i.e., list) and file (which is used to determine the type of any specified files). Likewise, when viewed in a GUI (graphical user interface), the icons for hard links are identical to those for their target files.

That the initial name of a file and all hard links to that file all share the same inode can be clearly seen by using the ls command with its -i (i.e., inode) option. Thus, for example, the following would show that the inode numbers of file1 and hlink1 from the above example are identical:

ls -i file1 hlink1

The number of hard links to any file is shown in the second column of output produced by using ls with its -l (i.e., long) option. It can be seen that the number is the sum of the target file and any hard links to it (i.e., the sum of the initial name and any subsequently added names) and that it is the same for the target and for each such link.

Hard linked files can also be found by using the find command with its -type f option (to select only regular files) followed by its -links +1 option (to show all regular files with more than one hard link to them) as follows:

find -type f -links +1

When a change is made to the contents of a file, the linkage to all of the hard links is preserved. However, some text editors may break the link by creating a new inode for the revised contents,1 and thus it can be prudent to check important links after modifying files.

The rm command superficially appears to remove or delete files. What it really does, however, is to reduce a file's hard link count (i.e., the number of names the file has) by one, and it does not directly affect the inode or the file's data. When the count reaches zero, the file appears to have vanished because there is no longer any easy way to reference it. However, the file's data is only truly deleted when the location(s) on the hard disk drive (HDD) or other storage media that contains it is overwritten by a new file.

Thus, for example, the following would remove the hard link hlink1 that was created in the above example:

rm hlink1

Using rm again with the one remaining name as follows would then make the file's data virtually inaccessible:

rm file1

Perhaps the most useful application for hard links is to allow files, programs and scripts (i.e. short programs) to be easily accessed in a different directory from the original file or executable file (i.e., the ready-to-run version of a program). Typing the name of the hard link will cause the program or script to be executed in the same way as using its original name.

Symbolic links, also called soft links, are more useful than hard links because they can be made to directories as well as to files on different filesystems and on different partitions. Moreover, when using a GUI, symbolic links have special icons that immediately identify them as being links rather than ordinary files. However, they have the disadvantage that they become unusable if their target file is deleted.

Aliases superficially resemble hard links in that they are another way of providing multiple names for any file. However, the alias command is built into the shell (i.e., the program that provides the text-only user interface) rather than being a separate program and the mechanism is very different from that of hard links. Like symbolic links, aliases can be used not only for files but also for directories and can cross filesystem and partition boundaries. In addition, an alias can be used as a short name for any shell text (i.e., a command or series of linked commands, inclusive of tbeir options and/or arguments).

1Tests on Red Hat Linux 9 found that hard links were broken when modifying files using the gedit text editor. However, they were not broken when using the vi and Abiword text editors as well as the KHexEdit hex editor on the same version of Linux. The failure of gedit to preserve hard links was due to the fact that it actually creates a copy of the modified file that it saves (and thus the new inode number) rather than making the changes to the original file, but this copy is given the name of the original file. However, a similar test using a newer version of gedit (2.14.0) on Fedora Core 5 showed that the problem had been corrected and that there was no breakage of links.

Created October 19, 2007.
Copyright © 2007 The Linux Information Project. All Rights Reserved.