Aliases and Symbolic Links


Sometimes you want users to be able to access a file by a different name, or from a different file-system location than the one in which it actually resides. You may have a script or program that depends on a file-system hierarchy that's different from what you have. You may have multiple versions of a document or folder and want a link called Latest to always point to the most current one.

You can use two types of files on Mac OS X to access files and folders indirectly. One is an alias, which you create in the Finder. The other is a symbolic link, which you create using the command line. Symbolic links are common on UNIX systems. Classic applications handle symbolic links correctly.

Some examples of symbolic links in Mac OS X exist because of an extra folder, called private, which was held over from NEXTSTEP. private contains other directories like etc, var, tmp, and Network. Mac OS X uses symbolic links for each of these folders and so the operating system thinks these folders actually exist at the root level of the boot volume, not one level deep inside private, where they actually exist.

Mac OS X makes liberal use of symbolic links. You can locate links using the find command:

find / type l ls


Aliases and symbolic links both act as pointers to files and folders, but their behavior is different. At the very basic level, the Finder understands both aliases and symbolic links but UNIX via the Terminal has no concept of aliases so they show up as a zero byte length file.

Using Aliases

You create an alias to a file or folder using Make Alias from the Finder's File menu. The following figure shows an alias to the folder AFP548. The arrow on the folder icon indicates it is an alias. If you use Get Info to display more information about the alias, you see that Kind is listed as Alias and that Original shows the location of the folder to which it points.

Move Original Files

If you move the original folder somewhere elsein this case, into the folder called EditedWebSitesand use Get Info to display information about the alias again, you'll see that the information has changed. Now Original shows the new location of the original folder. Mac OS X automatically updates aliases when the original file or folder is moved. Keep in mind that dragging a file or folder to the Trash is not deletingit's moving it to the Trash. The file still exists, and an alias will continue to point to it.

Alias data is stored in the resource fork of a file. You can use the techniques discussed earlier to look at the resource fork of an alias. To see the raw data stored about an alias, issue the following command:

/Developer/Tools/DeRez  YourAliasFile only alias


You can also get data using the strings command:

strings YourAliasFile/..namedfork/rsrc


You'll see some of the path information in the output.

Delete and Replace Original Files

If, instead of moving the original folder, you delete it from the computer completely (for example, by using rm from Terminal), Get Info will show that the alias no longer points to anything. If you re-create the folder in the original location, the alias will point to the new folder.

In Mac OS X v10.1, alias resolution was based upon an HFS Plus identifier called the file ID. Starting with Mac OS X v10.2, alias resolution attempts to resolve the file pathname before attempting to resolve the alias using the file ID. This behavior corrects a problem in Mac OS X v10.1 that prevented aliases from working as expected on volumes restored from a backup copy.

Creating Symbolic Links

Symbolic links originated in the UNIX world. You create a symbolic link using ln with the s option. In the following figure, a symbolic link is created to AFP548Site. A long listing shows what the link points to. Note that the file mode begins with the lowercase letter l.

$ ln -s AFP584Site AFP548Site_link $ ls -l drwxr-xr-x 2 david david 68 13 Jan 10:10 AFP584Site lrwx------ 1 david david 8 13 Jan 10:12 AFP548Site_link -> AFP584Site


Note

The above text is all on one line when viewed in the Terminal. We are unable to display it all on one line here.


$ ls -l AFP548Site/ total 56 -rw-r--r-- 1 david david 722 13 Jan 10:16 MyDoc.rtf -rw-r--r-- 1 david david 21190 13 Jan 10:17 MyPhoto.jpg $ ls -l AFP548Site_link/ total 56 -rw-r--r-- 1 david david 722 13 Jan 10:16 MyDoc.rtf -rw-r--r-- 1 david david 21190 13 Jan 10:17 MyPhoto.jpg


Move Original Files

When you look at a symbolic link in the Finder, it looks just like an alias. In fact, if you use Get Info to display more information, you will see that Kind is listed as Alias and that Original shows the location of the original folder. But something different happens when you move the original file somewhere else, as illustrated by the following figure.

First of all, the link's icon changes (it may take a Finder refresh or a log out to see the icon change). Additionally, when you use Get Info, you see that it still points to a file that does not exist and it cannot be reassociated with another original here, as the button Select New Original cannot be chosen.

Symbolic links are not updated when you move the original file or folder. A symbolic link that no longer points to anything is called a dangling symbolic link.

Note

If you try to change folders from a dangling symbolic link using cd from the command line, you will get an error.


Re-create an Original Path

A symbolic link is set to a fixed path when it is created. If you move or delete the original file and then place another file at the original location, the symbolic link will point to the new file. This can be useful, because you do not have to re-create the link when you replace the file. This is very useful when you update the files upon which the links rely.

Comparing Alias and Symbolic Links

The following table summarizes the differences between aliases and symbolic links. It lists how each one is created, how it appears in the Finder and the command line, and what happens when you move the file or directory pointed to.

 

Alias

Symbolic Link

Create with

Finder File Menu

ln s

Finder shows

Icon with arrow

Icon with arrow

 

Kind=Alias

Kind=Alias

ln s shows

Empty file

Leftmost letter = l link > original

Move original

Still points to original

After log out and log in dangling symbolic link

Move original and create new file in original location

Still points to original

Points to new file

Delete original

Dangling Alias

Dangling Symbolic Link

Delete original and create new file in original location

Points to new file

Points to new file





Apple Training Series. Mac OS X System Administration Reference, Volume 1
Apple Training Series: Mac OS X System Administration Reference, Volume 1
ISBN: 032136984X
EAN: 2147483647
Year: 2005
Pages: 258
Authors: Schoun Regan

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