About Links (the Unix Version of Aliases)

About Links (the Unix Version of Aliases)

A Mac alias is a special file that "points to" another file. Unix has its own way of doing this, using links . Actually, Unix has two kinds of links: symbolic links (or symlinks in the Unix vernacular) and hard links . Hard links are sometimes simply called links , and symlinks are sometimes called soft links .

Both symlinks and hard links are created with the ln command ( Table 5.6 ).

Table 5.6. Options for ln




Force the link to remove any existing file at the target location, if possible.


If the target path is a symbolic link, do not follow it. Use with the -f option to replace a symbolic link with a hard link.


Create a symbolic link instead of a hard link.

A hard link is virtually indistinguishable from the original file. It is simply a second name for the exact same chunk of data on your disk.

Symlinks are more similar to Mac aliases than hard links are, so we will address them in detail first.

Using symbolic links

Symlinks are special files that contain the path (relative or absolute) to another file.

When seen from the Finder, symlinks look and behave (mostly) like Mac aliases. (When Mac aliases are examined from the command line, they look like empty files unless you examine them with the GetFileInfo command described above, in which case the A attribute will be present.)

Mac aliases still point to the original file even if the alias and/or the original file is moved. Unix symlinks are less intelligent .

For instance, symlinks that use full paths can be moved around and still work (because a full path always points to the same place), but symlinks that use relative paths will stop working if they're moved (unless the link and original file move together and maintain the relative relationship). This is because symlinks point at a path, not at the actual file's data the way that Mac aliases do. If the symlink points to an original file that is moved, the symlink will stop working because the path no longer has the original file.

If you look at a symbolic link with ls -l , you see the path that the link points to. Figure 5.27 shows that the /etc directory is actually a symlink to /private/etc , and /mach is actually a symlink to /mach.sym .

Figure 5.27. You can make symlinks by using either full or relative paths.
 localhost:~ vanilla$  ls -ld /etc  lrwxrwxr-t  1 root             admin            11 Feb 25 17:40 /etc -> private/etc localhost:~ vanilla$  ls -l /mach  lrwxrwxr-t  1 root             admin            9 Feb 25 17:40 /mach -> /mach.sym localhost:~ vanilla$ 

The first example shows a symlink that points to a relative path, and the second example shows a symlink pointing to a full path.

You use the Unix ln command to create hard links and the -s option to create symbolic links. (Notice that the syntax of the ln command is similar to that of the cp command, covered earlier in this chapter.)

To create a symbolic link:

Type ln -s but don't press yet.

Type the path to the existing file or directory.

Think about whether you want to use a relative path or a full path for the existing file.

These are different commands:

 ln -s /Users/vanilla/Documents/  letter.doc . ln -s ../Documents/letter.doc . 

They both create a symlink called letter.doc in the current directory, but the first one points to an absolute path, while the second one points to "one step toward the root directory, then down into Documents, then letter.doc."

Remember, a full path continues to work even if the symlink is moved, and a relative path continues to work if the original and the symlink are moved togethersay if you copy a collection of directories and files to another computer.

Type the path or name of the symlink, and press .

If the path you type is an existing directory (for example, the current directory), then the symlink will have the same name as the original file and will be created in that directory.

ln -s ../file.txt .

creates a symlink called file.txt in the current directory. That symlink will point to ../file.txt (remember, . is just another name for the current directory; it is a perfectly valid path).

If you want to give the symlink a different name than the original, just type a pathname that ends in the new name (or, more simply, a new filename). The symlink will be created with that name:

ln -s ../file.txt newname


  • Use symlinks when you want it to be obvious where the links point. Anyone can use ls -l to see where a symlink points.

  • Use symlinks whenever you want to make a link to a directory.

  • Use symlinks when you want to link to a file on a different disk or partition.

  • Do not use symlinks when you want the link to keep working when the original file moves.

  • Unix tools deal with symbolic links in different ways, sometimes acting upon the symlink file itself, sometimes acting upon the original. For example, the -H , -L , -P , and -R options to cp all affect how cp handles symlinks. For this reason, it is important to be aware of the presence of symlinks even if you did not create them. For instance, the rm program never follows symlinks; it will remove them but not follow the link to remove the original file.

  • If you remove the original file, a symlink stops workingyou get a "File not found" error if you try to use it. But if you put a new file at the location that the symlink points to, then the symlink works, regardless of what file is there (even if it's not the original) This is different from the way Finder aliases work, even though from the Finder it is hard to distinguish an alias from a symlink (they appear as the same icon).

Using hard links

At the beginning of this chapter, we described how Unix directories are special files that contain a list of file and directory names . Filenames are stored as entries in directories. These entries are links between the filename and the actual physical location on the disk partition where the file is stored (called an inode ). Every file has at least one hard link. A hard link is essentially a name for a file, an entry in a directory. So a file's actual data occurs only once on the disk (at the location specified by its inode), but it can appear in more than one directory, or even twice in the same directory but with different names, which are called hard links .

Hard links are used partly for the same reasons as symbolic links (for example, as a space-saving mechanism), but, more important, they are used so that any of the links can be deleted and all the other links will still work (contrast this with symbolic links, where if the original file is deleted, the symbolic links will no longer point to anything).

Hard links differ from symbolic links in some important ways:

  • You cannot make a hard link to a directory.

  • You cannot make a hard link to a file on a volume that is different from the one containing the link.

  • Hard links continue to work even if the original file and/or the link is moved around on the disk.

  • If you move a hard link to a different disk or partition, it becomes a new file, no longer connected in any way to the original. That is, if you edit the link after moving it, the original file is not affected at all, and vice versa.

  • Perhaps most interestingly, hard links continue to work even if the original file is deleted. Now how can that be?

While symbolic links are actually tiny files that contain the path of the original, hard links are extra names for the original file. Every time a hard link is created or deleted, the operating system makes a note about it in the data stored on disk about the file.

Keeping in mind that filenames are actually hard links, when you use rm to remove a file, you are really removing a link, not a file. The "link count" is reduced by one. The file itself is not removed until the last hard link is removed. As long as at least one hard link for a file remains, the file will still exist.

You can see the number of hard links to a file with ls -l . Figure 5.22 shows where to find the number of links in the output from ls -l .

Because a hard link points to the actual location of a file on the disk (to the inode for the file), you cannot create a hard link on one disk that points to a file on another disk (or volume). For that, you need to use symlinks.

To create a hard link:

  • ln existingpath newpath

    With hard links, it makes no difference if the paths you use are relative or full. In either case, a new directory entry is created.

    If the new path you enter is an existing directory path, then the link is created inside that directory and has the same name as the original.


  • Use a hard link when you want the link to keep working even if the original file is moved or removed.

  • Just as with cp , if you supply ln with more than two arguments, it assumes the last argument is a directory and creates links inside that directory for each of the earlier arguments.

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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