18.4 More About LinksUNIX provides two different kinds of links:
You obviously can't do without copies of files: copies are important whenever users need their own "private version" of some master file. But it is also important to know about links. With links, there's only one set of data and many different names that can access it. Article 18.5 shows how to make links. 18.4.1 Differences Between Hard and Symbolic LinksWith a hard link, the two filenames are identical in every way. You can delete one without harming the other. The system deletes the directory entry for one filename and leaves the data blocks (which are shared) untouched. The only thing rm does to the inode is decrement its "link count," which (as the name implies) counts the number of hard links to the file. The data blocks are only deleted when the link count goes to zero - meaning that there are no more directory entries that point to this inode. Article 17.22 shows how to find the hard links to a file. With a symbolic link, the two filenames are really not the same. Deleting the link with rm leaves the original file untouched, which is what you'd expect. But deleting or renaming the original file removes both the filename and the data. You are left with a link that doesn't point anywhere. (Article 16.28 has a script that finds unconnected symlinks.) Remember that the link itself doesn't have any data associated with it. Despite this disadvantage, you rarely see hard links on UNIX versions that support symbolic links. Symbolic links are so much more versatile that they have become omnipresent. Let's finish by taking a look at the ls listing for a directory. This directory has a file named file with another hard link to it named hardlink . There's also a symlink to file named (are you ready?) symlink :
$ You've seen the -l option ( 22.2 ) and also, probably, the -a option ( 16.11 ) for listing "dot files." The -i option lists the i-number ( 1.22 ) for each entry in the directory ( 18.2 ) ; see the first column. The third column has the link count : this is the number of hard links to the file. When you compare the entries for file and hardlink , you'll see that they have a link count of 2. In this case, both links are in the same directory. Every other entry (i-number, size, owner, etc.) for file and hardlink is the same; that's because they both refer to exactly the same file, with two links (names).
A symbolic link has an 18.4.2 Links to a DirectoryWhile we're at it, here's a section that isn't about linking to files or making symbolic links. Let's look at the first two entries in the previous sample directory in terms of links and link counts. This should help to tie the filesystem together (both literally and in your mind!).
You've seen
$
Look at the i-numbers for the entries in the parent directory.
Which entry is for our current directory?
The entry for
sub
has the i-number 140330, and so does
the
Now you should be able see why every directory has at least two links.
One link, named
Every directory has a
Bzzzzzzt... time's up.
When a directory has subdirectories, it will also have a hard link named
As they should, all the links have the same i-number: 85523. Make sense? This concept can be a little abstract and hard to follow at first. Understanding it will help you, though - especially if you're a system administrator who has to use strong medicine like clri ( 23.13 ) . For more practice, make a subdirectory and experiment in it the way we've shown in this article.
By the way, directories and their hard links - , |
|