Datasheet

Task 1.3: Manage Links
19
the original name show 2, and so on. (For directories, the second column’s number refers to the
number of directories within the specified directory, including pointers to the directory itself and
its parent directory.)
Current versions of Linux forbid making hard links to directories, but this was
possible with some earlier versions of Linux.
Hard links to a single file may exist in two or more different directories; however, both
directories must exist on the same filesystem (partition or removable disk). Because hard links
are created by pointing two filenames at the same file data, it makes no sense to create hard
links across filesystems.
Creating Soft Links
Soft links (aka symbolic links) are an alternative to hard links. Instead of creating a duplicate
directory entry that points directly to the same underlying filesystem data, you are creating a new
file that contains the filename of the target file. As a consequence, it’s possible to point to files
across filesystems. To create a soft link, you use the ln command, but pass it the -s option:
$ ln -s sample-file another-link
$ ls -l
total 9
drwxr-xr-x 22 fred users 1184 May 25 12:51 X11
lrwxrwxrwx 1 fred users 11 May 26 15:30 another-link -> sample-file
-rw-r--r-- 2 fred users 2260 May 25 12:51 fstab
-rw-r--r-- 2 fred users 2260 May 25 12:51 sample-file
This example shows the result, including how soft links appear in directory listings. Note
that the link count in the second column of the listing doesn’t increase when a soft link is cre-
ated. The soft link itself, though, includes the l file type code in the permissions string and
shows the linked-to file after the filename in a long listing.
Unlike hard links, soft links don’t work quite exactly like the original link. The time to
access a soft link is minutely longer than the time to access the original file. You can delete the
soft link itself without affecting the linked-to file, but if you delete the original file, the soft link
will be broken; it will point to nothing. You can create soft links to directories.
In practice, soft links are more common than hard links. The fact that they can be created
across filesystems and the fact that they can point to directories makes them more flexible.
You should be cautious, though, not to break soft links by deleting, moving, or renaming the
original files.
Managing Links
The mv, rm, and cp commands work on links just as they work on the original files. Thus, link
management is just like ordinary file management; for instance, suppose you decide you only
83484.book Page 19 Monday, September 18, 2006 8:58 AM