Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
---------------------------------------
A link in a Linux file system provides a mechanism for making a connection between files. While comparisons can be made to shortcuts in Windows, this is not terribly accurate as linking in Linux has a lot more utility.
To get the full benefit from the description of links we should be passingly familiar with the previous discussion on files and inodes. We should recall that a Linux file consists of three parts;
- The filename and it’s associated inode number
- An inode that describes the attributes of the file
- The data associated with the file.
The file name and inode number point to an inode that has all the information about the file and in turn points to the data stored on the hard drive.
Links can be identified when listing files with ls -l
by a l
that appears as the first character in the listing for th file and by the arrow (->
) at the end that illustrates the link. For example by executing the following list command we can see (as only one amongst many files) that the file /etc/vtrgb
is a link to the file /etc/alternatives/vtrgb
.
Note the leading l
and link sign for vtrgb
in the output.
ls -l
by a l
that appears as the first character in the listing for th file and by the arrow (->
) at the end that illustrates the link. For example by executing the following list command we can see (as only one amongst many files) that the file /etc/vtrgb
is a link to the file /etc/alternatives/vtrgb
.l
and link sign for vtrgb
in the output.Soft Links (aka symbolic links, aka symlinks)
A soft link can also be referred to as a symbolic link, but is probably more frequently called a ‘symlink’. A symlink has a file name and inode number that points to it’s own inode, but in the inode there is a path that redirects to another inode that in turn points to a data block.
Soft Link |
Because inodes are restricted to the partition they are created on, a symbolic link allows redirecting to a path that can cross partitions.
If we edit the linked file, the original file will be edited. If we delete the linked file, the original file will not be deleted. But if we delete the original file without deleting the link, we will be left with an orphaned link.
Soft links can link to directories and files but if the file is moved the link will no longer work.
Hard Links
A hard link is one where more than one file name links to an inode.
Hard Link |
This means that two file names are essentially sharing the same inode and data block, however they will behave as independent files. For example if we then delete one of the files the link is broken, but the inode and data block will remain until all the file names that link to the inode are deleted.
Hard links only link to a file (no directories), cannot span partitions and will still link to a file even if it is moved.
I don't know if you are looking for comment. Anyhow, I think that, after having explained hard links, you should have mentioned that they are very rarely used. In fact, AFAIK no Linux desktop managers allows creating them or are they used by any distribution. They mostly exist for historical reasons at this point.
ReplyDeleteThanks Ricardo, Comments are really appreciated. It's very much a learning process, so every little bit helps.
Deletenice
ReplyDelete