Each file on your system is represented by an inode (for Information Node; pronounced “eye-node”). An inode contains all the information about the file. However, the inode is not directly visible. Instead, each inode is linked into the filesystem by one or more hard links. Hard links contain the name of the file and the inode number. The inode contains the file itself, i.e., the location of the information being stored on disk, its access permissions, the file type, and so on. The system can find any inode if it has the inode number.
A single file can have more than one hard link. What this means is that multiple filenames refer to the same file (that is, they are associated with the same inode number). However, you can’t make hard links across filesystems: All hard references to a particular file (inode) must be on the same filesystem. This is because each filesystem has its own set of inodes, and there can be duplicate inode numbers on different filesystems.
Because all hard links to a given inode refer to the same file, you can make changes to the file, referring to it by one name, and then see those changes when referring to it by a different name. Try this:
cd to your home directory and create a file called firstlink containing the word “hello.” What you’ve actually done is redirect the output of echo (echo just echoes back what you give to it), placing the output in firstlink. See the chapter on shells for a full explanation.
Confirms the contents of firstlink.
Creates a hard link: secondlink now points to the same inode as firstlink.
Confirms that secondlink is the same as firstlink.
Notice that the number of hard links listed for firstlink and secondlinkfiles!inodes is 2.
This is another shell redirection trick (don’t worry about the details). You’ve appended the word “change” to secondlink. Confirm this with cat secondlink.
firstlink also has the word “change” appended! That’s because firstlink and secondlink refer to the same file. It doesn’t matter what you call it when you change it.
Changes permissions on firstlink. Enter the command ls -l to confirm that permissions on secondlink were also changed. This means that permissions information is stored in the inode, not in links.
Deletes this link. This is a subtlety of rm. It really removes links, not files. Now type ls -l and notice that secondlink is still there. Also notice that the number of hard links for secondlink has been reduced to one.
Deletes the other link. When there are no more links to a file, Linux deletes the file itself, that is, its inode.
All files work like this – even special types of files such as devices (e.g. /dev/hda).
A directory is simply a list of filenames and inode numbers, that is, a list of hard links. When you create a hard link, you’re just adding a name-number pair to a directory. When you delete a file, you’re just removing a hard link from a directory.