10.2. Links

The best way to understand what's behind this notion of link is to take an example. Let's create a (regular) file:

$ pwd
/home/queen/example
$ ls
$ touch a
$ ls -il a
  32555 -rw-rw-r--    1 queen       queen              0 Dec 10 08:12 a

The -i option of the ls command prints the inode number, which is the first field on the output. As you can see, before we created file a, there were no files in the directory. The other field of interest is the third one, which is the link counter of the file.

In fact, the command touch a can be separated into two distinct actions:

But now, if we type:

$ ln a b
$ ls -il a b
  32555 -rw-rw-r--    2 queen       queen              0 Dec 10 08:12 a
  32555 -rw-rw-r--    2 queen       queen              0 Dec 10 08:12 b
$

we have created another link to the same inode. As you can see, we have not created any file named b, but instead we have just added another link to the inode numbered 32555 in the same directory named b. You can see on the ls -l output that the link counter for the inode is now 2, and no longer 1.

Now, if we do:

$ rm a
$ ls -il b
  32555 -rw-rw-r--    1 queen       queen              0 Dec 10 08:12 b
$

we see that even though we have deleted the "original file", the inode still exists. But now the only link to it is the file named /home/queen/example/b.

Therefore, an inode is linked if and only if it is referenced by a name at least once in any directory[1]. Directories themselves are also stored into inodes, but their link count, unlike all other file types, is the number of subdirectories within them. There are at least two links per directory: the directory itself (.) and its parent directory (..).

Typical examples of files which are not linked (ie, have no name) are network connections: you will never see the file corresponding to your connection to linux-mandrake website in your file tree, whichever directory you try. Similarly, when you use a pipe in the shell, the file corresponding to the pipe does exist, but it is not linked.

Notes

[1]

If this directory is on the same filesystem as the inode.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/