|
CEG 333: Introduction to UnixPrabhaker MatetiLinks |
The ln utility is used to create symbolic or hard
links to files. Consider the following command:
ln abc.txt xyz.dat
It establishes the new name xyz.dat (which should not have existed prior to this) as a hard link to abc.txt (which should exist already). These two names refer to the same file. In the directory's second column the same i-number appears. Note that we created the hard link in the same directory as the original file. This is not necessary. When a file is deleted, its hard link count decreases by 1. If the new count is 0, the file is deleted, i.e., all the space it occupied is reclaimed.
Unix symbolic links (also known as symlinks or soft links) are a special kind of file pointing at their target at the file system level. Opening a Unix symlink is just like opening the original file.
Symbolic links are conceptually pointers to pathnames. They can contain any path name. Deleting a symlink does not affect the original file, but if the target is deleted, the symlink becomes broken.
Note: hardlinks cannot cross filesystems: they must stay on the same partition and drive. They also cannot point to directories.
ln creates hard links by default and symlinks if
the -s option is given. Its syntax is analogous to
cp or mv:
ln [-s] SOURCE_FILE DESTINATION
ln [-s] SOURCE_FILES... DESTINATION_DIRECTORY
See man ln for more information about links.