previous contents up next

Unix for Advanced Users

6. Manipulating Files

6.1. Creating Files

6.1.5. ln

6.1.5.1. Symbolic links

Sometimes you have a file that needs to be visible from more than one location. For instance, you may have a program that needs to deposit its log files somewhere in the /var filesystem, but find that there is no room under /var to keep the actual logs. In that case, what you need is a stand-in that will take up minimal space, but that will point to the real file wherever it is located in the filesystem.

A link is just such a stand-in. There are two types of links. A symbolic link is a file that contains only information about other files. Whenever a program tries to open a symbolic link, the operating system examines the link, finds the target file, and opens the target instead. Because the link is a separate file, you can see both the link and the target in a directory listing. ls -l shows a link as a normal file followed by an ASCII arrow (->) . On the right side of the arrow is the paths to the target file.

To make a symbolic link, use the command ln as follows:

ln -s target_name link_name
When you rm link_name, you are deleting the link itself, not the target. Be careful, however: rm -r link_name will remove the target, not the link, if the target is a directory.

6.1.5.2. Hard links

It is also possible to link a file by creating a directory entry with the same inode number as the target file. (See the filesystems section for an explanation of inodes.) ln operates this way if it is used without the -s option. The problem with hard links is that, by their nature, they cannot be seen with ls -l. Once a hard link is made, the operating system has no way to distinguish the link from the original file. Using rm on a hard link will remove the link, but leave the files's contents reachable through the file entry, just as with symbolic links--but using rm on the original file will also leave the file's contents reachable, this time through the link. Furthermore, it is not possible to make a hard link to a directory. For these reasons, hard links are seldom used.

previous contents up next