Наши партнеры








Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

 Purchase  Copyright © 2002 Paul Sheer. Click here for copying permissions.  Home 

next up previous contents
Next: 16. Pre-installed Documentation Up: rute Previous: 14. Permission and Modification   Contents

Subsections

15. Symbolic and Hard Links

Very often, a file is required to be in two different directories at the same time. Think for example of a configuration file that is required by two different software packages that are looking for the file in different directories. The file could simply be copied, but to have to replicate changes in more than one place would create an administrative nightmare. Also consider a document that must be present in many directories, but which would be easier to update at one point. The way two (or more) files can have the same data is with links.

15.1 Soft Links

To demonstrate a soft link, try the following:

 
 
 
 
5 
 
 
 
 
10 
 
 
touch myfile
ln -s myfile myfile2
ls -al
cat > myfile
a
few
lines
of
text
^D
cat myfile
cat myfile2

Notice that the ls -al listing has the letter l on the far left next to myfile2, and the usual - next to myfile. This indicates that the file is a soft link (also known as a symbolic link or symlink) to some other file.

A symbolic link contains no data of its own, only a reference to another file. It can even contain a reference to a directory. In either case, programs operating on the link will actually see the file or directory it points to.

Try

 
 
 
 
5 
 
 
mkdir mydir
ln -s mydir mydir2
ls -al .
touch ./mydir/file1
touch ./mydir2/file2
ls -al ./mydir
ls -al ./mydir2

The directory mydir2 is a symbolic link to mydir2 and appears as though it is a replica of the original. Once again the directory mydir2 does not consume additional disk space--a program that reads from the link is unaware that it is seeing into a different directory.

Symbolic links can also be copied and retain their value:

 
 
 
cp mydir2 /
ls -al /
cd /mydir2

You have now copied the link to the root directory. However, the link points to a relative path mydir in the same directory as the link. Since there is no mydir here, an error is raised.

Try

 
 
 
rm -f mydir2 /mydir2
ln -s `pwd`/mydir mydir2
ls -al

Now you will see mydir2 has an absolute path. You can try

 
 
 
cp mydir2 /
ls -al /
cd /mydir2

and notice that it now works.

One of the common uses of symbolic links is to make mounted (see Section 19.4) file systems accessible from a different directory. For instance, you may have a large directory that has to be split over several physical disks. For clarity, you can mount the disks as /disk1, /disk2, etc., and then link the various subdirectories in a way that makes efficient use of the space you have.

Another example is the linking of /dev/cdrom to, say, /dev/hdc so that programs accessing the device file /dev/cdrom (see Chapter 18) actually access the correct IDE drive.

15.2 Hard Links

UNIX allows the data of a file to have more than one name in separate places in the same file system. Such a file with more than one name for the same data is called a hard-linked file and is similar to a symbolic link. Try

 
 
 
touch mydata
ln mydata mydataB
ls -al

The files mydata and mydataB are indistinguishable. They share the same data, and have a 2 in second column of the ls -al listing. This means that they are hard-linked twice (that there are two names for this file).

The reason why hard links are sometimes used in preference to symbolic links is that some programs are not fooled by a symbolic link: If you have, say, a script that uses cp to copy a file, it will copy the symbolic link instead of the file it points to. [ cp actually has an option to override this behavior.] A hard link, however, will always be seen as a real file.

On the other hand, hard links cannot be made between files on different file systems nor can they be made between directories.


next up previous contents
Next: 16. Pre-installed Documentation Up: rute Previous: 14. Permission and Modification   Contents