Links


A link is a pointer to a file. Every time you create a file by using vim or 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 some 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 (page 88). You may also have to change the access permission of the parent directory of the file to give the user read, write, or execute permission (page 90). Once the permissions are appropriately set, the user can create a link to the file so that each of you can access the file from your separate directory hierarchies.

A link can also be useful to a single user with a large directory hierarchy. You can create links to cross-classify files in your directory hierarchy, using different classifications for different tasks. For example, if you have the file layout depicted in Figure 4-2, you might have a file named to_do in each subdirectory of the correspond directorythat is, in personal, memos, and business. If you 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. You can then 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-12.

Figure 4-12. Using links to cross-classify files


Although it may sound complicated, this technique keeps all of 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 outdated. The section on hard links is marked as optional; you can skip it, although it discusses inodes and gives you insight into the structure of the filesystem.


Optional: Hard Links

A hard link to a file appears as another file. If the file appears in the same directory as the linked-to file, the links must have different filenames because two files in the same directory cannot have the same name. You can create a hard link to a file only from within the filesystem that holds that file.

ln: CREATES A HARD LINK

The ln (link) utility (without the s [symbolic] option) creates an additional hard link to an existing file using the following syntax:

ln existing-file new-link 


The next command makes the link shown in Figure 4-13 by creating a new link named /Users/zach/letter to an existing file named draft in Max's home directory:

Figure 4-13. Two links to the same file: /Users/zach/letter and /Users/max/draft


$ pwd /Users/max $ ln draft /Users/zach/letter 


The new link appears in the /Users/zach directory with the filename letter. In practice, Zach may need to change directory and file permissions for Max to be able to access the file.

The ln utility creates an additional pointer to an existing file but it does not make another copy of the file. Because there is only one file, the file status informationsuch as access permissions, owner, and the time the file was last modifiedis the same for all links; only the filenames differ. When Max modifies /Users/max/draft, for example, Zach sees the changes in /Users/zach/letter.

cp VERSUS ln

The following commands verify that ln does not make an additional copy of a file. Create a file, use ln to make an additional link to the file, change the contents of the file through one link, and verify the change through the other link:

$ cat file_a This is file A. $ ln file_a file_b $ cat file_b This is file A. $ vim file_b ... $ cat file_b This is file B after the change. $ cat file_a This is file B after the change. 


If you try the same experiment using cp instead of ln and change a copy of the file, the difference between the two utilities will become clearer. Once you change a copy of a file, the two files are different:

$ cat file_c This is file C. $ cp file_c file_d $ cat file_d This is file C. $ vim file_d ... $ cat file_d This is file D after the change. $ cat file_c This is file C. 


ls and link counts

You can use ls with the l option, followed by the names of the files you want to compare, to confirm that the status information is the same for two links to the same file and is different for files that are not linked. In the following example, the 2 in the links field (just to the left of zach) shows there are two links to file_a and file_b:

$ ls -l file_a file_b file_c file_d -rw-r--r-- 2 zach  zach  33  May 24 10:52 file_a -rw-r--r-- 2 zach  zach  33  May 24 10:52 file_b -rw-r--r-- 1 zach  zach  16  May 24 10:55 file_c -rw-r--r-- 1 zach  zach  33  May 24 10:57 file_d 


Although it is easy to guess which files are linked to one another in this example, ls does not explicitly tell you.

ls and inodes

Use ls with the i option to determine without a doubt which files are linked. The i option lists the inode (page 937) number for each file. An inode is the control structure for a file. (HFS+ filesystems do not have inodes but, through an elaborate scheme, appear to have inodes.) If the two filenames have the same inode number, they share the same control structure and are links to the same file. Conversely, when two filenames have different inode numbers, they are different files. The following example shows that file_a and file_b have the same inode number and that file_c and file_d have different inode numbers:

$ ls -i file_a file_b file_c file_d 3534 file_a    3534 file_b    5800 file_c    7328 file_d 


All links to a file are of equal value: The operating system cannot distinguish the order in which multiple links were created. When a file has two links, you can remove either one and still access the file through the remaining link. You can remove the link used to create the file and, as long as one link remains, still access the file through that link.


Symbolic Links

In addition to hard links, Mac OS X supports symbolic links, also called 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 filea pointer to the hard link to the file).

Advantages of symbolic 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.

Often the Mac OS X directory hierarchy is composed of several filesystems (volumes). Because each filesystem keeps separate control information (that is, separate inode tables or filesystem structures) for the files it holds, it is not possible to create hard links between files in different filesystems. A symbolic link can point to any file, regardless of where it is located in the file structure, whereas a hard link to a file must be in the same filesystem as the other hard link(s) to the file. When you create links only among files in your own directories, you will not notice this limitation.

A major advantage 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 link keeps alive even after a new file is created. In contrast, a symbolic link always points to the newly created file and does not interfere when you delete 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.

Finder aliases

A symbolic link is very similar to a Windows shortcut or a Macintosh Finder alias. However, a Finder alias is not a symbolic link and does not work with most shell commands. Conversely, a symbolic link is recognized by the Finder as though it were a Finder alias. A Finder alias is in some ways more similar to a hard link; a Finder alias points to a file, no matter where it is, rather than to a path, no matter what file is stored there.

ln: Creates a Symbolic Link

You use ln with the s option to create a symbolic link. The following example creates a symbolic link /tmp/s3 to the file sum in Zach's home directory. When you use an 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 -s /Users/zach/sum /tmp/s3 $ ls -l /Users/zach/sum /tmp/s3 -rw-rw-r--  1 zach  zach  13 Jun 12 09:51 /Users/zach/sum lrwxr-xr-x  1 zach  wheel  15 Jun 12 10:09 /tmp/s3 -> /Users/zach/sum $ cat /tmp/s3 This is sum. 


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, including a simple filename, 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 displays an error message.


$ pwd /Users/zach $ ln -s sum /tmp/s4 $ ls -l sum /tmp/s4 lrwxr-xr-x 1 zach  zach   3 Jun 12 10:13 /tmp/s4 -> sum -rw-rw-r-- 1 zach  wheel   38 Jun 12 09:51 sum $ cat /tmp/s4 cat: /tmp/s4: No such file or directory 


The sizes and times of the last modifications 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 s option, ln does not care whether the file you are creating a link to is an ordinary file or a directory.

Optional: cd and Symbolic Links

When you use a symbolic link as an argument to cd to change directories, the results can be confusing, particularly if you did not realize that you were using a symbolic link.

If you use cd to change to a directory that is represented by a symbolic link, the pwd shell builtin lists the name of the symbolic link. The pwd utility (/bin/pwd) lists the name of the linked-to directory, not the link, regardless of how you got there.

$ ln -s /Users/zach/grades /tmp/grades.old $ pwd /Users/zach $ cd /tmp/grades.old $ pwd /tmp/grades.old $ /bin/pwd /Users/zach/grades 


When you change directories back to the parent, you end up in the directory holding the symbolic link. Because /tmp is itself a symbolic link to /private/tmp, after you give a cd .. command, your working directory is /private/tmp.

$ cd .. $ pwd /tmp $ /bin/pwd /private/tmp 



rm: Removes a Link

When you create a file, there is one hard link to it. You can delete the file or, using UNIX 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 the space the file occupied on the disk for subsequent use by other files. 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 dragging a file to the Finder's Trash folder, this operation cannot be undone. A skilled hacker can sometimes piece the file together with time and effort.

When you remove all 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 zach  zach  981 May 24 11:05 sum $ ln -s sum total $ rm sum $ cat total cat: total: No such file or directory $ ls l total lrwxr-xr-x  1 zach  zach  3 May 24 11:09 total -> sum 


When you remove a file, be sure to remove all symbolic links to it. Remove a symbolic link in the same way you remove other files:

$ rm total 





A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net