Hard links#
A hard link is an other word for file name. For most Linux file systems it completely fine to have files with multiple names. The hard link counter keeps track of how many file names refer to a particular file. It it shown by stat
file
or ls -l
file
. Usually files have a hard link count of 1, i.e. the file has exactly one name. However directories usually a higher count for example dirA
, dirA/.
and dirA/dirB/..
are all different names for the same file which makes the hard link count at least 3.
Note
A hard link is very different from a soft link or symbolic link. The latter is an actual file of special type (link
) which has another filename as its content. While accessing a symbolic link its content is read and then used as target for the file access. The procedure is called following or dereferencing the link.
Directory Entries and Index Nodes#
A file name does not directly correspond to the data of the file, instead it refers to an object called dentry (directory entry) in the containing directory. Each a dentry points to an inode (index node) which then refers to the actual data. Multiple dentries may refer to the same inode. By this mechanism a file may be known by several names. All these dentries are equally ranking. There is no such thing as primary dentry. If a file is deleted its dentry is removed. The hard link counter in the corresponding inode is decreased. If the counter is still non-zero, there are other dentries referring to it and the inode stys intact. If the counter is zero, the inode and eventually the data is released.
dentries inodes data
+-------+ +----------------+
| fileA | ---> | metadata: | +-----------+
+-------+ | hard links:2 | ---> | file data |
+-------+ | permissions | +-----------+
| fileB | ---> | ctime, ... |
+-------+ +----------------+
Permissions, owner and other metadata is stored in the index nodes. That is why changing permissions or metadata is seen by using any file name.