soft links and hard links in Solaris
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.
In Solaris, there are two types of links:
- Soft (symbolic) links
- Hard links
Soft (symbolic) links
Sometimes symbolic links are used for shortcuts; other times we use them to link to a filename from each user's home directory to a centralized location or file.
For example, perhaps we want to have a common directory named
documents where every user stores their documents. This directory exists as
/export/data/documents. In each user's directory, we create a link named documents that points to
As a user, whenever I store something in the directory named
$HOME/documents, the file actually gets directed to
/export/data/documents. We can identify links when we perform a long listing on a directory as follows:
Note: Notice the use of the
-i option used with the
ls command and the results displayed. This option is used to display the inode number (in the left column of the output) that has been assigned to each file and directory. I'll describe why this inode number is relevant to links later in this section when I describe hard links.
$ ls -li $HOME 75264 drwxr--r-- 2 mchurchi staff 512 Jun 6 20:36 dir1 78848 drwxrwxr-x 2 mchurchi staff 512 Jun 6 20:38 dir2 82432 drw-r--r-- 2 mchurchi staff 512 Jun 6 20:39 dir3 3593 lrwxrwxrwx 1 mchurchi staff 22 Jun 17 17:09 documents -> /export/data/documents : :
Notice the file that has an l as the first character of column 2 indicates this is a soft or symbolic link. The sixth field shows a file size of 22 bytes and the last field shows which file or directory this link is pointing to. Each file has a unique inode number identified in column 1; the importance of this column is discussed later in this chapter.
When storing a file in $HOME/documents, the system is redirecting it to be stored in
/export/data/documents. Now when changing to the
/export/data/documents directory and issuing the
ls -li command:
$ cd /export/data/documents $ ls -li 125461 drwxr-xr-x 2 root other 512 Jun 17 17:09 documents
Notice that the file that has a d as the first character of column 2. This is the directory that the
$HOME/documents link points to. The first column shows the inode number, and the sixth column shows the file size as 512 bytes.
Symbolic links can point to files anywhere on the network. The file or directory could exist in another file system, on another disk, or on another system on the network.
The syntax for creating a symbolic link is as follows:
ln -s <source-file> <link-name>
For example, you might have a file named file1 that has the following contents:
This is the contents of file1
To create a symbolic link named link1, which will be linked to the existing file named file1, you issue the following command:
$ ln -s file1 link1
Now when you list the contents of the directory you see two files:
3588 -rw-r--r-- 1 mchurchi staff 30 Jun 17 17:51 file1 3594 lrwxrwxrwx 1 mchurchi staff 5 Jun 17 18:09 link1 -> file1
See the link named link1 pointing to file1? If you display the contents of link1, it shows the following:
$ cat link1 This is the contents of file1
If you remove file1, the source file, link1 will still exist, but it points to a file that does not exist and you get the error:
$ cat link1 cat: Cannot open link1
When you re-create file1, link1 will contain data again.
A hard link is more difficult to determine, because they are not so obvious when viewed with the ls -li command. For example, when you go to the
/etc/rc2.d directory and type:
$ ls -li total 102 : : 2123 -rwxr--r-- 5 root sys 1718 Jan 21 2005 S47pppd 2102 -rwxr--r-- 2 root sys 327 Jan 21 2005 S70uucp 1368 -rwxr-xr-x 2 root other 1558 Jan 9 2005 S72autoinstall 241 -rwxr--r-- 2 root sys 1262 Jan 21 2005 S73cachefs.daemon 1315 -rwxr--r-- 2 root sys 1028 Jan 21 2005 S81dodatadm.udaplt 237 -rwxr--r-- 2 root sys 256 Jan 21 2005 S89PRESERVE 2103 lrwxrwxrwx 1 root root 31 Aug 10 09:49 S89bdconfig ->\ ../init.d/buttons_n_dials-setup 1898 -rwxr--r-- 5 root sys 3506 Jan 10 2005 S90wbem 2100 -rwxr--r-- 5 root sys 1250 Jan 10 2005 S90webconsole : :
The first character in the second column of information displays the file type as a regular file (-). The third column, link count, shows a number greater than 1. It displays the number of links used by this inode number. These are hard links, but they are not identified as links and the display does not show which file they are linked to.
Think of a hard link as a file that has many names. In other words, they all share the same inode number. Looking at the file named S90wbem in the previous listing, we see an inode number of 1898. List all file names in this file system that share this inode number as follows:
# find / -mount -inum 1898 -ls 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
All six of these files have the same inode number; therefore, they are the same file. You can delete any one of the filenames, and the data will still exist.
In this example, the file is named file1 in $HOME:
$ ls -l file1 3588 -rw-r--r-- 3 mchurchi staff 30 Jun 17 17:51 file1 $ cat file1 This is the contents of file1
The syntax for creating a hard link is as follows:
ln <source-file> <link-name>
To create a hard link named
link1, which will be linked to the existing file named file1, issue the following command:
$ ln file1 link1
Now when I list the contents of the directory, I see two files:
3588 -rw-r--r-- 2 mchurchi staff 30 Jun 17 17:51 file1 3588 -rw-r--r-- 2 mchurchi staff 30 Jun 17 17:51 link1
Both files share the same inode number, the number of links is two, and the file size is 30 bytes. If I display the contents of
link1, it shows the following:
$ cat link1 This is the content of file1
If I remove
file1, the source file,
link1 still exists and still contains the data. The data will not be deleted until I destroy the last file that shares this inode number.
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 3588 in the
/export/home file system may not even exist in the /var 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. Here's what happens when you create a symbolic link to a file that does not exist:
Removing a Link
Remove a link using the rm command as follows:
For example, to remove the link named
$ rm link1
Note: Removing Files and Links When you remove a file, it's always a good idea to remove the symbolic links that pointed to that file, unless you plan to use them again if the file gets re-created.
Another advantage of symbolic links over hard links is that a symbolic link can link directories or files, whereas a hard link can link only files.