Again, quoted from Ars Technica:
***
Symbolic links and hard links
Mac OS has included the ability to create an "alias" of a file since System 7. Aliases are small, plain files whose contents are interpreted by the Finder and other Mac OS APIs, allowing the original file to be found. To the core OS in Mac OS X, however, aliases are meaningless. They're literally just small, plain files. (In fact, their size is shown as zero because all the data is in the resource fork.) The first part of this lesson is that aliases have absolutely nothing to do with symbolic links and hard links.
For decades, Unix has had two ways to do something similar to what aliases do: symbolic links and hard links. A symbolic link, also called a symlink, is a pointer to another file. The location of this file is specified using an absolute or relative path string. Symbolic links are created with the ln command using the -s flag. Here's an example.
% ls -l
-rw-r--r-- 1 john staff 894 Oct 18 10:04 myfile
% ln -s myfile mysymlink
% ls -l
-rw-r--r-- 1 john staff 894 Oct 18 10:04 myfile
lrwxr-xr-x 1 john staff 6 Oct 18 10:06 mysymlink -> myfile
As indicated by the leading "l" in the Unix permissions string, mysymlink is a symbolic link. It points to myfile, which the ls command shows to the right of the little ASCII arrow. Note that the symbolic link is tiny: only six bytes. That's not a coincidence. Symbolic links are "dumb" in that they literally contain nothing more than the target file path as a string. In this case, the string is the relative path "myfile" which (surprise) is exactly six characters long.
If I move mysymlink to an entirely different directory that also happens to contain a file named myfile, then the symlink will point to that file instead. If I delete myfile, the symlink will remain, still containing that same path string which no longer leads to an actual file. Observe:
% rm myfile
% cat mysymlink
cat: mysymlink: No such file or directory
The error reported is a bit confusing. The mysymlink file still exists. But when the operating system attempts to open that file, it dutifully follows the symbolic link and finds that there is no file at the path specified by the link. The cat command then reports the error it encountered when trying to open the file it was originally told to open.
This property of symlinks can also be considered a feature in some circumstances: "simple" rather than "dumb." But sometimes a more robust mechanism is needed. Enter hard links.
A hard link is simply a reference to some data on disk. Think of a file as a combination of a name and a pointer to some data. Deleting a file really means deleting the name portion of that duo. When there are no more names pointing to a particular piece of data disk, then that disk space can be reused.
Most files have just one name. In effect, every plain file is a hard link. Take another look at the myfile listing from before.
-rw-r--r-- 1 john staff 894 Oct 18 10:04 myfile
See that number "1" right before the word "john"? That indicates that the data linked to the name myfile has only one name. In other words, deleting myfile will drop the count to zero, allowing the disk space previously used by myfile to be used for other purposes. Note that creating a symlink does not increment this number.
% ls -l
-rw-r--r-- 1 john staff 894 Oct 18 10:04 myfile
lrwxr-xr-x 1 john staff 6 Oct 18 10:06 mysymlink -> myfile
In fact, the symlink itself has a "1" in that column, indicating that there is only one name linked to those the six bytes of data. Now let's make a hard link, again using the ln command, but this time with no flags.
% ln myfile myhardlink
% ls -l
-rw-r--r-- 2 john staff 894 Oct 18 10:04 myfile
-rw-r--r-- 2 john staff 894 Oct 18 10:04 myhardlink
lrwxr-xr-x 1 john staff 6 Oct 18 10:06 mysymlink -> myfile
Now the link count for both myfile and myhardlink is two. This indicates that the data linked to by myfile has two names, and the data linked to by myhardlink has two names. In this case, both link to the same data. We have no way of knowing that merely by looking at the link counts; we know because we just ran the ln command ourselves.
Though the size for both myfile and myhardlink is listed as 894 bytes, those 894 bytes exist only once on the disk. Again, it's one chunk of data with two names. As far as the OS is concerned, neither myfile nor myhardlink is the "real" file. They are both equally real, as real as either one would be if the link count was one instead of two.
Since these two files link to the same data, the results of modifying that data will be reflected in each file. For example, let's add five bytes to the chunk of data linked to myfile (the word "test" plus a newline character):
% cat >> myfile
test
^D
Now let's look at the result.
% ls -l
total 24
-rw-r--r-- 2 john staff 899 Oct 18 10:38 myfile
-rw-r--r-- 2 john staff 899 Oct 18 10:38 myhardlink
lrwxr-xr-x 1 john staff 6 Oct 18 10:06 mysymlink -> myfile
The sizes of myfile and myhardlink are now both reported as 899 (894 + 5). Meanwhile, mysymlink is still the lone name linked to its six bytes of data containing the string "myfile".