Creating a link to a directory
It is often useful to change to another directory without typing its full pathname: symbolic links provide a useful shortcut to do this. A symbolic link differs from a hard link. It is a small file that contains a reference (by name) to a directory or file that already exists. Unlike normal links, symbolic links can cross filesystems and link to directories. (They are used extensively by the system.) Also unlike normal links, symbolic links are separate files; they cease to work if the file they point to is deleted or renamed, or if they are moved.
Many of the files found in /bin, /lib, and /usr are actually symbolic links that point to files (of the same name) stored below /var/opt. The directories these files are located in are called “storage sections”. Storage sections are used because they make it easier to install system upgrades. Software subsystems (such as UUCP) consist of many files, which may be installed in several directories. However, all the files in a subsystem belong to a single storage section. By overwriting the contents of the (single) storage section directory, all the files in the subsystem can be updated simultaneously.
Symbolic links are identified in a directory listing by a “->”, as follows:
$ l drw-r--r-- 1 johnd unixdoc 29 Feb 27 15:56 mydata -> /u/work group/tasks/project/01
You can obtain a directory listing without symbolic links visible in it by specifying the -L (logical) option. This makes ls (or l, or any related program) list the directory, replacing the information about each symbolic link with the details for the file pointed to by the link:
$ l -L drw-r--r-- 1 johnd unixdoc 10297 Feb 27 15:56 mydata
To create a symbolic link, use the ln -s option, as follows:
ln -s directory symbolic_link
For example, suppose you work in /u/workgrp/tasks/projects and your home directory is /u/me. Your normal command to work on a file would be the following:
$ cd /u/workgrp/tasks/projects
To reduce the typing required, enter the following command:
$ ln -s /u/workgrp/tasks/projects mydata
This command creates a symbolic link called mydata in your current directory. From now on, mydata and /u/workgrp/tasks/projects refer to the same location, and you can relocate to /u/workgrp/tasks/projects by typing cd mydata instead of typing in the full pathname.
You must have write permission on a directory before you can create a link that involves that directory or a file in that directory.