What is a Link in Linux ?
A link in Linux is pointers pointing to a file or a directory.
When do we use Links in linux ?
well there are a lot more examples which we will discuss further in this article but for now a few uses are to create a convenient path to a directory buried deep within the file hierarchy; other uses for links include:
- Linking libraries
- Making sure files are in constant locations (without having to move the original)
- Keeping a “copy” of a single file in multiple locations
Wait a second so are they just as same as “Short -cuts” ?🧐
In a way, yes…but as Gandalf says
Let me explain. Say, for instance, you have an external drive, attached to your Windows machine. On that drive is a folder called Homework😛. If you create a shortcut to the directory on your desktop, when you click to open the shortcut, your file manager will open to the Music directory on your external drive.
Now, say you have that drive attached to a Linux machine. That drive is mounted to say, /data and on that drive is the folder Homework. You create a link to that location in your home directory — so you how have a link from ~/Homework that points to /data/Homework. If you open the shortcut in your home directory, it opens the file manager in ~/Homework, instead of /data/Homework. Any changes you make in ~/Homework will automatically be reflected in /data/Homework. And that is the big difference.
Types of Links
In Linux there are two different types of links:
- Hard links
- Symbolic links
Can only link to files (and not directories)
Cannot reference a file on a different disk or volume, and they reference the same inode as the original source.
A hard link will continue to remain usable, even if the original file is removed.
Command to create a hard link is:
$ ln [original filename] [link name]
SymLink or Symbolic Link
Can link to directories
Reference a file/folder on a different disk or volume
Will exist as a broken (unusable) link if the original location is deleted
Reference abstract filenames and directories (as opposed to physical locations), and are given their own, unique inode.
- Command to create a Soft link is:
$ ln -s [original filename] [link name]
The primary difference between hard and symbolic link creation, is that you use the -s option.
What is an inode you ask , An inode is a data structure that stores various information about a file in Linux, such as the access mode (read, write, execute permissions), ownership, file type, file size, group, number of links, etc. Each inode is identified by an integer number. An inode is assigned to a file when it is created
One interesting thing of note is how inodes are treated the different types of links. the inode for the hard links are the same, whereas the inodes for the symbolic links are different. This can be further illustrated by removing the original location of the symbolic link. When you do that, the soft link goes away (although the broken referral link file remains behind). Why? The reference inode the symbolic link pointed to no longer exists.
Unlike with hard links, if you delete the original file or directory, the symbolic link will remain, however it will now be considered a broken link and will be unusable. Remember, with hard links, you can remove the original and the link will remain and still be usable.