| Add comments here | |
|
|
| |
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 simple be copied, but this would create an
administrative nightmare to have to replicate changes in more
than one place. The way two files can have the same data is
with links.
|
| |
Try
5
10
|
touch myfile
ln -s myfile myfile2
ls -al
cat > myfile
a
few
lines
of
text
^C
cat myfile
cat myfile2
|
|
| |
You will notice that the ls -al listing has the letter
l on the far left next to myfile2 while 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 does now work.
|
| |
One of the common uses of symbolic links is to make
mounted (see Section 22.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
sub-directories 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
(see Chapter 21.1) file /dev/cdrom,
actually access the correct IDE drive.
|
| |
|
| |
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 very similar to a symbolic link. Try
|
touch mydata
ln mydata mydata2
ls -al
|
The files mydata2 and mydata2 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, say, have a script that uses cp
to copy a file, it will copy the symbolic link instead of the
file it points to18.1. A hard link however will always be seen as a
real file.
|
| |
Hard links however cannot be made between files on different
file-systems. They also cannot be made between directories.
|