Project19.Understand Links and Aliases


Project 19. Understand Links and Aliases

"What's with symbolic links, hard links, and Finder aliases?"

This project explains Unix links, the difference between hard and symbolic links, and how Finder aliases fit in. It also covers the ln command.

Get a Few Pointers

A link is one file that points to another. The link points to the original file, and accessing the link usually accesses the original file. Editing the link will edit the original file. Some commands do not follow links by default. The rm command, for example, doesn't follow links. This is a good thing; otherwise, there'd be no way to delete a link without deleting the original file.

Links come in two varieties: symbolic links (sometimes called soft links) and hard links. As you'll see, each type has advantages and limitations.

Create Soft Links

Here is an example of creating a symbolic link.

We have a file called orig. Let's make a symbolic link to that file, using the ln command and specifying option -s for symbolic. Specify the original file and a name for the link, in that order.

$ ln -s orig sym-pointer


Use ls and option -lor -F to show the symbolic link as a link.

$ ls -l -rw-r--r-- 1 saruman saruman 23 ... orig lrwxr-xr-x 1 saruman saruman 4 ... sym-pointer -> orig $ ls -F orig            sym-pointer@


Next, display both the original file and the link file. The files should contain the same text. If you edit the original file and display both files again, the link file will have changed too.

$ cat orig I am the original file $ cat sym-pointer I am the original file


Tip

To remove the symbolic link home, which links to the directory /Users/saruman, type

$ rm home


not, as would be gotten by tabbed completion,

$ rm home/



How do symbolic links work? File sym-pointer is a file that contains the relative pathname of the target file orig and is flagged as a link. When a soft link is followed, the pathname is read from the file and replaces the pathname of the symbolic link.

If you move orig, the link will break. If you move sym-pointer to another directory, the link will also break; the pathname is relative, not absolute.

$ mv orig orig.bak $ cat sym-pointer cat: sym-pointer: No such file or directory


You can make a link to a file in another directory by giving a pathname (either relative or absolute) as the first argument.

$ ln -s /Users/saruman home $ ls -l lrwxr-xr-x 1 saruman saruman 14 ... home -> /Users/saruman ...


An interesting point to note: In this example, we have used an absolute pathname in /Users/saruman. Therefore, the symbolic link home will contain an absolute pathname. If the symbolic link is moved, the link will not be broken, unlike in the first example. If the original file is moved, however, the link will be broken.

Create Hard Links

A hard link is different from a symbolic link in that it isn't a file; it's a new directory entry. Both the original and the new directory entries point to the same file. In fact, Unix uses an i-node to describe a file, and both directory entries mention the same i-node. Here's an example of a hard link.

$ ln orig hard-pointer $ ls -l -rw-r--r-- 2 saruman saruman 23 ... hard-pointer -rw-r--r-- 2 saruman saruman 23 ... orig lrwxr-xr-x 1 saruman saruman 4 ... sym-pointer -> orig


Note

Applying the rm command to a hard-linked file may not actually delete the file. Only its directory entries are removed until the last link to the file has been deleted. Only then will rm delete the file itself.


Passing option -i to ls tells it to display a file's i-node number. Here, you can see that both hard-pointer and orig have the same i-node number : 999822.

$ ls -i 999822 hard-pointer     999822 orig     999825 sym-pointer


Both directory entries have equal claim over the file, and (other than my obvious naming conventions) it's not possible to say which is the original file and which is the hard link. Unlike a soft link, a hard link will not break when either of the linked files is moved, because although the directory entries may change, the i-node number and the file itself remain unchanged. However, hard links cannot point across file systems (disks, partitions, or mounted file systems), because i-node numbers are unique within a file system but not across file systems.

When a hard-linked file is copied, a new file is created, not a new hard link. If you clone a file system by copying all files, the cloned version will have separate files instead of hard links.

$ cp hard-pointer new-file$ ls -i 999822 hard-pointer   999829 new-file  999822 orig ...


Symbolic Links vs. Hard Links

Use hard links instead of symbolic links when the linked files may move or be renamed, and you wish the link to remain intact.

Use symbolic links instead of hard links when the link spans disks, partitions, or file systems, or when you wish the link to be recognizable as a link.

Learn More

Refer to "Recursion" in Project 2 if you are unfamiliar with recursion.


Links and Other Commands

Many commands take options -H, -L, and -P, which generally mean the following:

  • -H. If option -R is specified, symbolic links on the command line are followed. (Symbolic links encountered in the tree traversal are not followed.)

  • -L. If option -R is specified, all symbolic links are followed.

  • -P. If option -R is specified, no symbolic links are followed.

Such commands include chflags, chgrp, chmod, chown, cp, du, and find.

Aliases

A Finder alias is like a hard link in that renaming does not break it, but it can span file systems. Aliases are not recognized or followed by Unix because the link information is held in the resource fork, and Unix commands do not recognize resource forks. To Unix, an alias looks like an empty file. The Finder does recognize Unix links, however.

Here, we have created a Finder alias called orig alias pointing to orig.

$ ls -l -rw-r--r-- 2 saruman saruman 23 ... hard-pointer -rw-r--r-- 2 saruman saruman 23 ... orig -rw-r--r-- 1 saruman saruman 0 ... orig alias lrwxr-xr-x 1 saruman saruman 4 ... sym-pointer -> orig


Displaying orig alias does not follow the alias to orig.

$ cat orig I am the original file $ cat orig\ alias





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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