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; echo "hello" > firstlink
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.
cat firstlink
Confirms the contents of firstlink.
ln firstlink secondlink
Creates a hard link: secondlink now points to the same inode as
firstlink.
cat secondlink
Confirms that secondlink is the same as firstlink.
ls -l
Notice that the number of hard links listed for firstlink and
secondlinkfiles!inodes is 2.
echo "change" >> secondlink
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.
cat firstlink
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.
chmod a+rwx firstlink
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.
rm firstlink
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.
rm secondlink
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.