Soft and Hard Links… Explained

A friend and I were kind of reviewing some HPUX/Solaris commands and ideas. She asked “What were the difference between hard and soft links?”. Unable to get the answer at once, I of course consulted my Solaris Administration guide. I also got curious cause I really don’t mind what their difference is, all I care about is that they were linked and that’s it. So here goes the difference between Mr Hard and Ms. Soft Link….

First, what are links? A link is a pointer to another file or directory. Links provide a mechanism for multiple file names to reference the same data on disk.

Soft (Symbolic) Links is a shortcut (like a desktop shortcut on Windows). The syntax for creating a symbolic link is as follows:

ln -s source-file link-name

Now when you list the contents of the directory you see two files:

3588 -rw-r--r--   1 chelsy staff       30 Jun 17 17:51 file1
3594 lrwxrwxrwx   1 chelsy staff       5  Jun 17 18:09 link1 -> file1

Hard link is more difficult to determine, because they are not so obvious when viewed with the ls -li command. The syntax is :

ln file1 link1 (No –s?)

What you could notice is that the inode in each file is the same when you list them. Notice the first column, all of which have 1898 value, meaning the files in the list have the same inode number, therefore, is the same file.

1898  4 -rwxr--r--   5 root   sys    3506 Jan 10  2005 /etc/init.d/init.wbem
1898  4 -rwxr--r--   5 root   sys    3506 Jan 10  2005 /etc/rc0.d/K36wbem
1898  4 -rwxr--r--   5 root   sys    3506 Jan 10  2005 /etc/rc1.d/K36wbem
1898  4 -rwxr--r--   5 root   sys    3506 Jan 10  2005 /etc/rc2.d/S90wbem

1898 4 -rwxr–r– 5 root sys 3506 Jan 10 2005 /etc/rcS.d/K36wbem

So what is the difference then? Except for the syntax with no –s in the option, try deleting the source file in the soft link.

In Soft Link, if you remove file1, the source file, link1 will still exist, but it points to a file that does not exist. So when you open link1,

cat link1

The cat command can’t print out the contents of the file, so you get this message:

cat: Cannot open link1

In Hard Link, if you remove file, the source file, link1 will still exist and you will still be able to open it. The data will not be deleted until you destroy the last file that shares this inode number. Nice huh???

Pros and Cons???

A hard link cannot span file systems; it can only point to another file located within its file system. The reason is that hard links all share an inode number. Each file system has its own set of inode numbers; therefore, a file with inode number 1234 in the /export/home file system may not even exist in the /usr file system.

An advantage of a symbolic link over a hard link is that you can create a symbolic link to a file that does not yet exist. You cannot create a hard link unless the source file already exists.

Makes sense?

One thought on “Soft and Hard Links… Explained”

Leave a Reply

Your email address will not be published. Required fields are marked *