Unix Power ToolsUnix Power ToolsSearch this book

10.7. Linking Directories

One feature of symbolic links (Section 10.5) (a.k.a. symlinks) is that unlike hard links, you can use symbolic links to link directories as well as files. Since symbolic links can span between filesystems, this can become enormously useful.

For example, sometimes administrators want to install a package in a directory tree that's not where users and other programs expect it to be. On our site, we like to keep /usr/bin pure -- that is, we like to be sure that all the programs in /usr/bin came with the operating system. That way, when we install a new OS, we know for sure that we can overwrite the entirety of /usr/bin and not lose any "local" programs. We install all local programs in /usr/local.

The X11 package poses a problem, though. Our X windows package (discussed in Chapter 5) expects X11 programs to be installed in /usr/bin/X11. But X isn't distributed as part of our OS, so we'd prefer not to put it there. Instead, we install X programs in /usr/local/X11/bin and create a symbolic link named /usr/bin/X11. We do the same for /usr/include/X11 and /usr/lib/X11:

# ln -s /usr/local/X11/bin /usr/bin/X11
# ln -s /usr/local/X11/lib /usr/lib/X11
# ln -s /usr/local/X11/include /usr/include/X11

By using symlinks, we installed the package where we wanted, but we kept it invisible to any users or programs that expected the X programs, libraries, or include files to be in the standard directories.

Directory links can result in some unexpected behavior, however. For example, let's suppose I want to look at files in /usr/bin/X11. I can just cd to /usr/bin/X11, even though the files are really in /usr/local/X11/bin:

% cd /usr/bin/X11
% ls -F
       mkfontdir*      xcalc*          xinit*          xset*
   ...

But when I do a pwd,[42] I see that I'm really in /usr/local/X11/bin. If I didn't know about the symlink, this might be confusing for me:

[42]I mean the standard Unix pwd command, an external command that isn't built into your shell. Most shells have an internal version of pwd that "keeps track" of you as you change your current directory; it may not give the same answer I show here. You can run the external version by typing /bin/pwd.

% pwd
/usr/local/X11/bin

Now suppose I want to look at files in /usr/bin. Since I did a cd to /usr/bin/X11, I might think I can just go up a level. But that doesn't work:

-F Section 8.3

% cd ..
% ls -F
bin/     include/     lib/
% pwd
/usr/local/X11

What happened? Remember that a symbolic link is just a pointer to another file or directory. So when I went to the /usr/bin/X11 "directory," my current working directory became the directory to which /usr/bin/X11 points, which is /usr/local/X11/bin.

As a solution to this problem and others, the X distribution provides a program called lndir. lndir makes symlinks between directories by creating links for each individual file. It's cheesy, but it works. If you have it, you can use lndir instead of ln -s:

Figure Go to http://examples.oreilly.com/upt3 for more information on: lndir

# lndir /usr/local/X11/bin /usr/bin/X11
# ls -F /usr/bin/X11
X@       mkfontdir@      xcalc@          xinit@          xset@
   ...

-- LM



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.