Submind YouTube summaries
Thumbnail for Symbolic Links

Symbolic Links

Watch on YouTube

Video summary

The video introduces symbolic links as an advanced alternative to hard links for creating shortcuts within a file system. While hard links function like multiple names pointing directly to the same phone number in a contact list, symbolic links operate differently by storing the actual name of the target file or directory inside their own separate entry. This distinction is crucial because it allows symbolic links to reference anything, including files located on different partitions or even directories themselves, overcoming the limitations where hard links cannot span across partition boundaries. To demonstrate practical usage, the presenter uses the command line tool `ln` with the `-s` flag to create these links, showing that a symbolic link appears as a distinct file with permissions starting with an 'L'. When accessed, the system reads the name stored within the link and follows it to the original destination, making the shortcut function seamlessly for users. The video illustrates this by creating links for files with long names like "dihydrogen monoxide.txt" and directories such as "laws of thermodynamics," proving that these shortcuts work effectively regardless of whether they point to a file or a folder anywhere in the system. A significant advantage of symbolic links is their independence from the original data, which becomes clear when examining deletion scenarios. Removing the symbolic link itself does not delete the original file it points to, just as deleting a phone number from a contact list doesn't erase the actual phone number. Conversely, if the original file or directory is deleted, the symbolic link remains but becomes broken, attempting to access a non-existent target and resulting in an error message. This behavior highlights that symbolic links are merely references containing text strings rather than direct connections to data blocks. In conclusion, the video emphasizes that symbolic links offer a more modern and powerful solution compared to hard links due to their flexibility and ability to cross partition limits. Although they require slightly more processing steps to resolve the target name, this extra work solves many of the constraints associated with traditional linking methods. The presenter advises viewers to prefer symbolic links in most situations because they provide greater versatility for managing file system shortcuts without risking accidental data loss when manipulating the links themselves.
Read the full video transcript
In the previous video, we discussed hard links. If you haven't seen that video yet, now would be a good time to look at it or review it. When using hard links to make shortcuts, we have several entries in the inode table referring to the same inode or index node. Using our analogy of a contact list on your phone, a hard link is like having multiple names that can refer to the same phone number. Whether I want to look up Carl Frederick Brown, or as everyone calls him for short, Fred, I go directly to that phone number. There's another way to handle duplicate entries, though. Instead of having Fred, the shortcut, connect to a phone number, we will make a special entry that has the name of the person whose phone number we want, Carl Frederick Brown. In this new system, to look up Carl Frederick Brown, as before, it's a direct connection to his phone number. But when we want to look up Fred, we get to the special entry, and use that entry to look up the name it refers to, and then follow it to the phone number. Fred is a link that contains a name, which is a symbol for a phone number, and that's why it's called a symbolic link. These symbolic links look like a lot of extra work to get to their destination, and it is some extra work. But it also solves our problems with hard links. Since a symbolic link is a name, it can be the name of anything. We can make a link that leads to a directory, not just a file. And again, since it's just a name, it can name any file anywhere in the file system. We're no longer depending on I nodes that cannot connect across partitions. Okay, this has all been very theoretical. Let's go to the shell and make some symbolic links and see how they work. Here's a directory with some very long file names. And I'd like to make a shortcut, a symbolic link for this file dihydrogen monoxide.txt. I'm going to type ln to create a link and then the long option symbolic to create a symbolic link the name of the file I want to link to and the name of the shortcut that I want for it. The link file water.txt. Let's show the long listing for the directory now along with the I node numbers. And let's concentrate on our link file water.txt. First of all, unlike a hard link, its I node number is not the same as the I node number that it links to. That means that water.txt is a separate file. Its permissions start with the letter L meaning it's a symbolic link. The length of the file happens to be exactly the number of characters in the original file name dihydrogen monoxide.txt. And that's because water.txt really contains the name of the linked file. The linkage works great. If I show the contents of the original file or the link file I get the same result because the link eventually leads us to the original. Now I can do links to directories. I can make a link a symbolic link. The original directory is laws of thermodynamics. And the name that I want for it is thermo. And here's what our directory looks like now. I can then use the shortcut and CD thermo and then list what's in that directory or going back to my main directory here I can CD to laws of thermodynamics and do an LS and again get the same result because the link is taking me to the correct destination. I've returned to my main directory here and I'm going to make one more symbolic link. I'm going to use the short form of the option -s for symbolic link to a file that's on a different partition. And I'm going to name the shortcut web.html. Which results in this. There's my web.html that points to a file on a different partition. And if I see what's in that file, it accesses it no problem. There's one more issue to discuss in regard to symbolic links. Deleting links and the files that they link to. Here's our current status. Because a symbolic link is a separate file containing the linked file's name, we can delete the symbolic link and the file that it linked to is still there. And I can show you its contents. What about the other way around? First, let's recreate the link. Let's make the symbolic link again between dihydrogen monoxide.txt and water.txt. And it's back. This time instead of getting rid of the link file, I'm going to get rid of the original file that we're linking to. So, I'm going to remove dihydrogen monoxide.txt. And now when I do an ls -l, we see that the link file water.txt is still there, but it contains the name of a file that no longer exists. And if I try to show the contents of water.txt, it's going to try and follow it to dihydrogen monoxide.txt and say, "Sorry, no such file or directory." In summary, a symbolic link is a separate file. It contains the name of the file we're linking to. And you can make a link to a file or directory anywhere in the file system. Deleting a symbolic link does not affect the original file that it links to. Deleting the original file will leave the link file, which will contain the name of a file that no longer exists. And finally, when given a choice between hard links and symbolic links, go with the more modern, more powerful option. Use symbolic links.