Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

A brief tutorial on symbolic links UNIX
OS X's file structure mounts all partitions under the "/Volumes" directory at the root level of the filesystem. However, when navigating the filesystem with "cd" and other commands, it can be annoying to type "/Volumes/volume_name" each time you want to access a different partition. To learn about symbolic links and use them to add shortcuts at the root level of your filesystem, read the rest of this article. This assumes you are moderately comfortable in the Terminal, and that you have administrative privileges.

What is a Symbolic Link?
If you've ever made an "Alias" to a file in classic Mac OS, or a "Shortcut" to a file in Windows, you will easily be able to understand the UNIX equivalent (where "aliases" and "shortcuts" came from in the first place!), called a "Symbolic Link". The easiest definition to understand is directly from the man page, "[A Link] is useful for maintaining multiple copies of a file in many places at once without using up storage for the 'copies'; instead, a link 'points' to the original copy." (If you want to read more about it, type man ln in the Terminal.)

Let's say you have two partitions or drives - "X" containing OS X and "Classic" containing OS 9. To navigate to "X", you simply type
cd /
To navigate to "Classic", though, you'd have to type
cd /Volumes/Classic
If you have many partitions and/or drives, and you are trying to manage many files across them, it can get annoying to type "/Volumes" every time. Moreover, you cannot simply create an Alias to the drive/partition to accomplish this because the command line utility "cd" does not handle Mac OS aliases properly. However, a symbolic link will solve the problem.

To create the link, simply type the following:
cd /
ln -s /Volumes/Classic/ Classic
That's it. ln -s makes a new file called Classic which points to /Volumes/Classic/. To see that this worked, you can simply type
ls -la | more
(note the "|" character is called "pipe" and is found above the "Return" key). You should see a line with the following text:
Classic -> /Volumes/Classic/
The arrow shows you exactly what you did... A file named Classic points to /Volumes/Classic.

Links have many possibilities around the filesystem, and I encourage you to read more about them in the man page. While command line utilities cannot recognize Mac OS aliases, the Mac OS Finder will recognize symbolic links you construct in the terminal, or via Third-Party Utilities such as SNAX so you can use links just like aliases in the Finder.

Jordan
    •    
  • Currently 4.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[119,835 views]  

A brief tutorial on symbolic links | 9 comments | Create New Account
Click here to return to the 'A brief tutorial on symbolic links' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
re: symbolic links/aliases
Authored by: rusto on Tue, Nov 6 2001 at 1:57PM PST
thx for the tip, while fiddling with symbolic links and comparing them to the traditional Mac way of making aliases (command-option-drag) I discovered that the terminal-created symbolic link may be smaller. Do this: make an alias of a folder in the finder then make a symbolic link of that same folder. Then look at the file size in Get Info. I'm not exactly sure what the second figure is but I see this: Finder alias: Symbolic link: Note that while their size "on disk" are both 4k, the symbolic link has only 9 bytes to the right of that while the alias has 650 bytes. Does anyone know what this second figure represents?

[ Reply to This | # ]
re: symbolic links/aliases
Authored by: jmil on Tue, Nov 6 2001 at 2:25PM PST
This has to do with the HFS+ file structure. HFS+ has a minimum file size of 4KB. This means that any file you create (even a text file with just the letter "a", only 1 byte large) will take up a minimum block of 4 kilobytes. Isn't this inefficient? Well, yes and no. It's inefficient because you have wasted space. However, it makes finding files easier (because the blocks of data are much larger than one byte), and nearly all files in the old Mac OS were much larger than 4 KB anyway. Now with OS X, we have several hundred thousand files, but most are still larger than 4KB and the cost of hard drives is so amazingly cheap now that it wouldn't matter anyway. I hope that helps to clear things up.

Jordan

[ Reply to This | # ]
re: symbolic links/aliases
Authored by: normcook on Tue, Nov 6 2001 at 2:27PM PST
that size in bytes is how much space the file really needs. however, the smallest amount of space that can be allocated for something is 4k. so 9 bytes or 650 bytes, it is going to use 4k of disk space.

[ Reply to This | # ]
How to hide your links.
Authored by: serversurfer on Tue, Nov 6 2001 at 3:13PM PST
By *NIX convention, file whose name begins with a period are 'hidden'. The Finder honors this, so if you name your link '.Classic', instead of 'Classic', you won't see an alias to Classic when you look in your 'X' volume in the Finder or when you ls /. (use ls -a /)

Bonus Tip!:
This will add a character you have to type when you cd, but if you use the right naming convention, you can use tab-complete to make it faster than ever! Do this:

cd /
ln -s /Volumes/Classic .qclassic
cd ~
cd /.qc
[TAB]

The last line means type '/.qc' then press tab (with no space in between). I prefixed the volume name with 'q' because it stands for 'quick' and no other files there are likely to be called .qsomething. I used a lower case 'c' because tab-complete is case-sensitive by default.

[ Reply to This | # ]
yet another way...
Authored by: charlietuna on Tue, Nov 6 2001 at 3:36PM PST
this provides similar functionality, but not exactly the same. i prefer it because it keeps my root folder uncluttered. as it is i already have enough "TheVolumeSettings" and "Desktop DB" stuff that makes it look unelegant... but to the tip:

in tcsh you can set a variable called "cdpath" that includes a list of directories which are searched whenever you "cd" to another directory. perhaps this also would apply to pushd as well.

currently, you can type "cd Music" anywhere in your filesystem and if you aren't in a directory that contains a directory named Music, you will be switched over to ~/Music. if you do this in your .cshrc:

set cdpath = (~/ /Volumes)

you can do the same thing. if you have a partition named Classic, you can type "cd Classic" and you'll be put at /Volumes/Classic. tab completion does not work here, so you'll have to type the full name yourself.


[ Reply to This | # ]
Thanks!
Authored by: foamy on Tue, Nov 6 2001 at 3:54PM PST
That was very cool. Using it already.

[ Reply to This | # ]
yet another way...
Authored by: jmil on Tue, Nov 6 2001 at 4:01PM PST
Fantastic!

[ Reply to This | # ]
Important differences with aliases
Authored by: half on Tue, Nov 6 2001 at 8:11PM PST
aliases:

* keep pointing to the same file/folder, even when it is moved
(although not when moved to a different partition / drive)
* store a separate copy of the icon and can have different icons
* not usable from the command line

sym links:

* just store the location of the original. If it moves, they break.
If something else of the same name is put there it now points to that.
* uses the icon of the original
* Did not use to work in Classic / OS9, but seem to now - is this a 9.2 change?


The most important bit of this to remember is that aliases are pointers to objects and
symbolic links are pointers to locations. For most tasks, I think aliases are thus more useful.

half.

[ Reply to This | # ]
Important differences with aliases
Authored by: owain_vaughan on Wed, Nov 7 2001 at 6:21AM PST
Of course UNIX has an equivalent to aliases as well. Rather than using symbolic links you can use 'hard' links. Just use the ln command without the -s option and you will create a hard link. Same caveats as Mac aliases - only work on the same partition, but the cool thing is, both the 'original' and the new hard link are entirely equivalent. You can delete the original, but the file will still exist, because there is another 'link' to it. You can have as many hard links as you like, and as long as you have one the file will still exist.

[ Reply to This | # ]