To create a hard link instead of a symbolic link, use the
command without the
% ln filename linkname
When you create a hard link, it is identical in every way to the original file.
The hard link is simply another name for the same file. In fact, it is misleading to think of one name as the hard link and one as the
. Both names refer to the same piece of data stored on the filesystem, so both names are equally valid. After you create a hard link, if you delete the original
file you do not lose the data.
The data are lost only after you delete the original
file and all hard links to it. By contrast, when you create a symbolic link, you have two different files. You have the original file, which
contains the actual data. You also have the symbolic link, which contains only the name of the first file and acts as a pointer to it. If you delete the symbolic link, you do not lose the actual data.
But if you delete the original file, you lose the data, and the symbolic link points nowhere. This is an important limitation of symbolic links. However, symbolic links have two advantages over hard links:
First, symbolic links can cross file systems while hard links cannot. In other words, a symbolic link can point to a file that is stored in a different disk partition.
This is an important capability in todays complex computing environments, where UNIX systems typically use several file systems to store their data. Second, symbolic links can point to a directory, but hard links cannot.
For example, suppose you often work in the directory
You could create a symbolic link named
that points to that directory. Then, you could treat
as a shortcut to the longer directory name. In other words, you can apply commands such as
, and these commands would act on the actual directory.
Inodes are associated with precisely one directory entry at a time. However, with hard links it is possible to associate multiple directory entries with a single inode. To create a hard link use ln command as follows:
The commands below create a link to file1.