Links

Sometimes you would like to have a single file in two directories, just copy it in the 2nd directory is a bad idea, since it creates sooner or later a version problem: One version gets updated the other not. Sometimes you want to access a file under a different name as for/dev files and to hide the version number in library file names.

This is possible by creating links to such files using the ln command.

Note

In some cases a similar effect can be done using bind mounting directories

There are two kind of links:

Symbolic links

Symbolic links are often used to deal with versions. The different files or directories contain often a version number in their name. Instead of renaming the actual files or directory, just a symbolic link not containing any version number in its name is pointing to the selected file or directory containing the version number. Links are therefore used to select the desired versions.

Symbolic links are commonly used unfortunately they are not the default for ln (maybe due to historic reasons and backward compatibility).

Those links are made by:

ln -s<path to existing file or directory><name of the link>

ls -li shows in the first column the I-node number, this is a unique reference where the data is on the hard disk. The file or directory pointed to and the link have different I-nodes. Note how small the size of the link is. If you delete the file or directory pointed to, the link still remains but is useless since it points to nowhere.

It happens often that symlinks point to nowhere due to destination removals and forgotten symlinks.

Instead of absolute links: ln -s /lib/modules/3.5.7-gentoo/source -> /usr/src/linux-3.5.7-gentoo relative links do not get broken when the directory containing both (link and destination) gets moved: ln -s /lib/modules/3.5.7-gentoo/source -> ../../../usr/src/linux-3.5.7-gentoo

Static links

Are done without the -s option

ln<path to existing file or directory><name of the link>

ls -li shows in the first column for both the same I-node additionally the third column should show now a two, telling how many file names point to the single I-node. Therefore it can not be distinguish anymore what was the original and what was the link. Additionally I-nodes are just unique per file system, therefore static links are restricted to one single medium whereas symbolic links can spread even throughout the network. Deleting one file deletes just the filename and decrease the number in the third column except when it was the last filename pointing to the I-node then of course the date is deleted.


Linurs Servernest startpage