3.3.5. Linking files
3.3.5.1. Link types
Since we know more about files and their representation in the file system, understanding links (or shortcuts) is a piece of cake. A link is nothing more than a way of matching two or more file names to the same set of file data. There are two ways to achieve this:
- Hard link: Associate two or more file names with the same inode. Hard links share the same data blocks on the hard disk, while they continue to behave as independent files.
There is an immediate disadvantage: hard links can't span partitions, because inode numbers are only unique within a given partition.
- Soft link or symbolic link (or for short: symlink): a small file that is a pointer to another file. A symbolic link contains the path to the target file instead of a physical location on the hard disk. Since inodes are not used in this system, soft links can span across partitions.
The two link types behave similar, but are not the same, as illustrated in the scheme below:
Figure 3-2. Hard and soft link mechanism
Note that removing the target file for a symbolic link makes the link useless.
Each regular file is in principle a hardlink. Hardlinks can not span across partitions, since they refer to inodes, and inode numbers are only unique within a given partition.
It may be argued that there is a third kind of link, the user-space link, which is similar to a shortcut in MS Windows. These are files containing meta-data which can only be interpreted by the graphical file manager. To the kernel and the shell these are just normal files. They may end in a .desktop or .lnk suffix; an example can be found in ~/.gnome-desktop :
[dupont@boulot .gnome-desktop]$cat La\ Maison\ Dupont [Desktop Entry] Encoding=Legacy-Mixed Name=La Maison Dupont Type=X-nautilus-home X-Nautilus-Icon=temp-home URL=file:///home/dupont
This example is from a KDE desktop:
[lena@venus Desktop]$cat camera [Desktop Entry] Dev=/dev/sda1 FSType=auto Icon=memory MountPoint=/mnt/camera Type=FSDevice X-KDE-Dynamic-Device=true
Creating this kind of link is easy enough using the features of your graphical environment. Should you need help, your system documentation should be your first resort.
In the next section, we will study the creation of UNIX-style symbolic links using the command line.
3.3.5.2. Creating symbolic links
The symbolic link is particularly interesting for beginning users: they are fairly obvious to see and you don't need to worry about partitions.
The command to make links is ln . In order to create symlinks, you need to use the -s option:
ln -stargetfilelinkname
In the example below, user freddy creates a link in a subdirectory of his home directory to a directory on another part of the system:
freddy:~/music>ln -s /opt/mp3/Queen/ Queenfreddy:~/music>ls -l lrwxrwxrwx 1 freddy freddy 17 Jan 22 11:07 Queen -> /opt/mp3/Queen
Symbolic links are always very small files, while hard links have the same size as the original file.
The application of symbolic links is widespread. They are often used to save disk space, to make a copy of a file in order to satisfy installation requirements of a new program that expects the file to be in another location, they are used to fix scripts that suddenly have to run in a new environment and can generally save a lot of work. A system admin may decide to move the home directories of the users to a new location, disk2 for instance, but if he wants everything to work like before, like the /etc/passwd file, with a minimum of effort he will create a symlink from /home to the new location
* License
* Introduction to Linux Index
SHARE