In Linux, a link is a way to assign multiple identities to a file, similar to Windows shortcuts. There are some reasons why Linux uses the links:
• it helps files to become more accessible;
• to give commands/files more names;
• to allow programs using the same files to access them from different places.
There are two types of links: hard links and symbolic links.
Hard links are obtained by creating two entries that target the same file (more precisely, the same inode – an inode contains information about the file to which it is associated: permissions, the physical address of the file on the disk, the file size, the number of references to itself, etc.). Both filenames are equally valid; there is no more valid file than the other. To delete the file, we also need to delete the hard links (the file system checks how many names it points to a certain inode and it is considered that the file has been deleted only when there are no more targets to the same content).
All native Linux filesystems support hard links, while some non-Linux filesystems can not support them. Because of the way they are created, hard links can only be created within the same filesystem (this may be a disadvantage) – for example, we can not create a hard link on the root partition (/) on a different filesystem that is mounted in a directory.
Symbolic links are links to the file name, actually representing a special file type, the link being retained in a new inode. Linux knows that we want to access the target file when we access the symbolic link. Thus, accessing a symbolic link is like accessing the original file. Symbolic links can also be created to files on other filesystems – we can target the root filesystem (/) to another filesystem mounted in a directory.
Symbolic links are much more used than hard links, the ability to target different filesystems representing a major advantage.
A disadvantage of symbolic links is that the search process for accessing the original file consumes more time, making it slower than accessing hard links.
Creating links in Linux with the ln command
The syntax of the In command is similar to the cp command:
In [options] source link
The source is represented by the original file to which we want to create the link.
Link is the name of the new link you created.
The ln command creates hard links by default; it supports some options, the most used being:
–f or –force – if there are other links to the source file, the command ln deletes all these old links
–s or –symbolic – creates symbolic links
There are several options that can perform more or less obscure tasks. Use man ln to see them.
$ ls -l
$ -rw-r–r– 1 mvps users 0 Dec 18 10:35 example /we notice that the number of links to the file is 1
$ ls example hard-link /creates a hard link to the example file
$
$ ls -l
$-rw-r-r– 2 mvps users 0 Dec 18 10:35 example /we notice that the number of links to the file has increased by 1
$
$ ln -s example symbolic-example /creates a symbolic link to the example file
Unlike hard links, the symbolic ones do not increase the count of the number of links to a file (see command ls -l); Also, ls -l command shows, conventionally denoted by an arrow (->), to which file a symbolic link is targeted.