|< Day Day Up >|
A link is a pointer to a file. Each time you create a file using vim, touch, cp, or any other means, you are putting a pointer in a directory. This pointer associates a filename with a place on the disk. When you specify a filename in a command, you are indirectly pointing to the place on the disk that holds the information you want.
Sharing files can be useful when two or more people are working on the same project and need to share information. You can make it easy for other users to access one of your files by creating additional links to the file.
To share a file with another user, first give the user permission to read from and write to the file. (You may also have to change the access permission of the parent directory of the file to give the user read, write, and/or execute permission.) Once the permissions are set appropriately, the user can create a link to the file so that each of you can access the file from your separate file trees.
A link can also be useful to a single user with a large file tree. You can create links to cross-classify files in your file tree, using different classifications for different tasks. For example, if your file tree is the one depicted in Figure 4-2, you might have a file named to_do in each subdirectory of the correspond directory that is, in personal, memos, and business. If you later find it difficult to keep track of everything you need to do, you can create a separate directory named to_do in the correspond directory and link each subdirectory's to-do list into that directory. For example, you could link the file named to_do in the memos directory to a file named memos in the to_do directory. This set of links is shown in Figure 4-13.
Figure 4-13. Using links to cross-classify files
Although it may sound complicated, this technique keeps all your to-do lists conveniently in one place. The appropriate list is easily accessible in the task-related directory when you are busy composing letters, writing memos, or handling personal business.
tip: About the discussion of hard links
Two kinds of links exist: hard links and symbolic (soft) links. Hard links are older and becoming dated. The section on hard links is marked as optional; you can skip it, although it discusses inodes and gives you insight into how the filesystem is structured.
In addition to hard links, Linux supports links called symbolic links, soft links, or symlinks. A hard link is a pointer to a file (the directory entry points to the inode), whereas a symbolic link is an indirect pointer to a file (the directory entry contains the pathname of the pointed-to file a pointer to the hard link to the file).
Limitations of hard links
Symbolic links were developed because of the limitations inherent in hard links. You cannot create a hard link to a directory, but you can create a symbolic link to a directory. A symbolic link can point to any file, regardless of where it is located in the file structure, but a hard link to a file must be in the same filesystem as the other hard link(s) to the file.
Often the Linux file hierarchy is composed of several filesystems. Because each filesystem keeps separate control information (that is, separate inode tables) for the files it contains, it is not possible to create hard links between files in different filesystems. When you create links only among files in your own directories, you will not notice these limitations.
One of the big advantages of a symbolic link is that it can point to a nonexistent file. This ability is useful if you need a link to a file that is periodically removed and re-created. A hard link keeps pointing to a "removed" file, which the hard link keeps alive even after a new file is created. A symbolic link always points to the newly created file and does not interfere with deleting the old file. For example, a symbolic link could point to a file that gets checked in and out under a source code control system, a .o file that is re-created by the C compiler each time you run make, or a log file that is periodically archived.
Although they are more general than hard links, symbolic links have some disadvantages. Whereas all hard links to a file have equal status, symbolic links do not have the same status as hard links. When a file has multiple hard links, it is analogous to a person having multiple full legal names, as many married women do. In contrast, symbolic links are analogous to nicknames. Anyone can have one or more nicknames but these nicknames have a lesser status than legal names. The following sections describe some of the peculiarities of symbolic links.
ln: Creates a Symbolic Link
Use ln with the symbolic (or s) option to create a symbolic link. The following example creates the symbolic link /tmp/s3 to the file sum in Alex's home directory. When you use the ls l command to look at the symbolic link, ls displays the name of the link and the name of the file it points to. The first character of the listing is l (for link):
$ ln --symbolic /home/alex/sum /tmp/s3 $ ls -l /home/alex/sum /tmp/s3 -rw-rw-r-- 1 alex alex 38 Jun 12 09:51 /home/alex/sum lrwxrwxrwx 1 alex alex 14 Jun 12 09:52 /tmp/s3 -> /home/alex/sum $ cat /tmp/s3 This is sum.
The sizes and times of the last modification of the two files are different. Unlike a hard link, a symbolic link to a file does not have the same status information as the file itself.
Similarly you can use ln to create a symbolic link to a directory. When you use the symbolic option, ln does not care whether the file you are creating a link to is a regular file or a directory.
tip: Use absolute pathnames with symbolic links
Symbolic links are literal and are not aware of directories. A link that points to a relative pathname, which includes simple filenames, assumes that the relative pathname is relative to the directory that the link was created in (not the directory the link was created from). In the following example, the link points to the file named sum in the /tmp directory. Because no such file exists, cat gives an error message:
$ pwd /home/alex $ ln --symbolic sum /tmp/s4 $ ls -l sum /tmp/s4 lrwxrwxrwx 1 alex alex 3 Jun 12 10:13 /tmp/s4 -> sum -rw-rw-r-- 1 alex alex 38 Jun 12 09:51 sum $ cat /tmp/s4 cat: /tmp/s4: No such file or directory
rm: Removes a Link
When you create a file, there is one hard link to it. You can delete the file or, using Linux terminology, remove the link with the rm utility. When you remove the last hard link to a file, you can no longer access the information stored there and the operating system releases for use by other files the space the file occupied on the disk. The space is released even if symbolic links to the file remain. When there is more than one hard link to a file, you can remove a hard link and still access the file from any remaining link. Unlike in DOS and Windows, there is no easy way in Linux to undelete a file once you have removed it. A skilled hacker can sometimes piece the file together with time and effort.
When you remove all the hard links to a file, you will not be able to access the file through a symbolic link. In the following example, cat reports that the file total does not exist because it is a symbolic link to a file that has been removed:
$ ls -l sum -rw-r--r-- 1 alex pubs 981 May 24 11:05 sum $ ln -s sum total $ rm sum $ cat total cat: total: No such file or directory $ ls -l total lrwxrwxrwx 1 alex pubs 6 May 24 11:09 total -> sum
When you remove a file, be sure to remove all symbolic links to it. Remove a symbolic link the same way you remove other files:
$ rm total
|< Day Day Up >|