Figure 13-3. A symlink to inode 7033
Now if Barney chooses to read
/home/barney/poems/carroll, he gets the same
data as if he had opened
/home/barney/poems/dodgson, because the system
follows the symbolic link automatically. But that new name
isn't the "real" name of the file, because (as you
can see in the diagram) the link count on inode 7033 is still just
one. That's because the symbolic link simply tells the system,
"If you got here looking for carroll, now
you want to go off to find something called
dodgson instead."
Since a soft link could point to a file that doesn't yet exist,
it could be used when creating a file as well. Barney has most of his
files in his home directory, /home/barney, but
he also needs frequent access to a directory with a long name that is
difficult to type:
/usr/local/opt/system/httpd/root-dev/users/staging/barney/cgi-bin.
So he sets up a symlink named
/home/barney/my_stuff, which points to that long
name, and now it's easy for him to get to it. If he creates a
file (from his home directory) called
my_stuff/bowling, that file's real name is
/usr/local/opt/system/httpd/root-dev/users/staging/barney/cgi-bin/bowling.
Next week, when the system administrator moves these files of
Barney's to
/usr/local/opt/internal/httpd/www-dev/users/staging/barney/cgi-bin,
Barney just repoints the one symlink, and now he and all of his
programs can still find his files with ease.
It's normal for either /usr/bin/perl or
/usr/local/bin/perl (or both) to be symbolic
links to the true Perl binary on your system. This makes it easy to
switch to a new version of Perl. Say you're the system
administrator, and you've built the new Perl. Of course, your
older version is still running, and you don't want to disrupt
anything. When you're ready for the switch, you simply move a
symlink or two, and now every program that begins with
#!/usr/bin/perl will automatically use the new
version. In the unlikely case that there's some problem,
it's a simple thing to replace the old symlinks and have the
older Perl running the show again. (But, like any good admin, you
notified your users to test their code with the new
/usr/bin/perl-7.2 well in advance of the switch,
and you told them that they can keep using the older one during the
next month's grace period by changing their programs'
first lines to #!/usr/bin/perl-6.1, if they need
to.)
Perhaps suprisingly, both hard and soft links are very useful. Many
non-Unix operating systems have neither, and the lack is sorely felt.
On some non-Unix systems, symbolic links may be implemented as a
"shortcut" or an "alias" -- check the
perlport manpage for the latest details.
To find out where a symbolic link is pointing, use the
readlink function. This will tell you where the
symlink leads, or it will return undef if its
argument wasn't a symlink:
my $where = readlink "carroll"; # Gives "dodgson"
my $perl = readlink "/usr/local/bin/perl"; # Maybe tells where perl is