Links

Sometimes it is desired to have the same file in two directories, 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 it is just desired to access a file under a different name as used in ls -l /usr/lib 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 created as:

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 the file or directory pointed to gets deleted, the link still remains but is useless since it points to nowhere (dangling links).

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

Hard links

In Linux file systems files are stored at a I-node. I-nodes can have more than one file name. So the files appears more than once but occupies just one time space on the drive and all files have consistent data.

Important

Even this looks as a wonderful concept, it has some significant drawbacks and therefore hard links are not widely used.

It cannot be easily seen if a file is a copy or a hard link and unfortunately copies are quickly done. File synchronizer, version management tools, file managers, file systems as from Microsoft not supporting hard links might create copies.

Having copies means that sooner or later the data in the files will no more be consistent.

If a disaster happens then it is a lot of work to clean it up and then next disaster might already be on the horizon

Hard 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.

ls -i <filename> shows the I-node number

find <directory where to find> -inum <I-node> looks in a directory for files having the same I-node

find . -samefile <path to filename> looks in the current directory to files that are hard-linked

find <directory> -samefile <path to filename> looks in a directory to files that are the hard-linked


Linurs startpage