Submind YouTube summaries
Thumbnail for Using the ln command to create links (shortcuts)

Using the ln command to create links (shortcuts)

Watch on YouTube

Video summary

The video explains how to create shortcuts for long file names using hard links, which are a fundamental concept in understanding the Linux file system. Unlike symbolic links, hard links function by creating multiple entries in the inode table that all point to the exact same data on the disk. When a hard link is created between two files, they share the same inode number, meaning they are essentially the same file accessible under different names. This mechanism is managed through a link count; as long as there is at least one link pointing to the data, the file content remains intact on the storage device. The tutorial demonstrates that deleting a hard link does not immediately erase the file's contents but simply removes one reference to it. The actual data is only deleted when the link count drops to zero, indicating that no links remain pointing to that specific inode. For example, if you create two hard links for a single file and then remove the original filename, the content persists because the remaining link still holds a reference. However, once the last link is removed, the operating system recognizes that the data is no longer needed by any user or program and cleans it up automatically. Despite their utility, hard links have significant limitations that prevent them from being used in all scenarios. First, they cannot be created for directories; attempting to make a hard link to a folder results in an error because the filesystem does not allow this operation. Second, hard links are restricted to files within the same partition or device. If you try to create a link between a file on one partition and another file on a different partition, the system will reject the request with an "invalid cross-device link" error. These constraints highlight why hard links are less flexible than symbolic links, which can point to directories and span across different partitions. In conclusion, while hard links offer an efficient way to manage long filenames and ensure data persists until all references are removed, their inability to link directories or cross partitions makes them unsuitable for many practical use cases. The video emphasizes that understanding these underlying mechanics is crucial for grasping how Linux manages storage, but the limitations necessitate the use of symbolic links for more versatile linking needs. The discussion sets the stage for future content that will explore how symbolic links overcome these specific restrictions to provide greater flexibility in file management.
Read the full video transcript
Sometimes we want to create shortcuts for long file names. We do this by creating links. There are two types of links. There's hard links, the older type, and symbolic links, the newer type. In this video, we'll talk about the older type of link, the hard link. Now, why am I talking about the old style at all? because it introduces some important information about how the Linux file system works. Consider these long file names. This is part of what you'd see with ls-l. This column tells the file name. This column tells the file size. This column tells how many links there are to the file. And the first column are the file permissions. First, we're going to investigate what's going on behind the scenes on your disk when you have these files. Each partition on your hard disk contains something called an index node table. And these index nodes are called iodes. It's sort of like a contact directory on your phone that associates each file with an index node on the disk that stores information about the file. If I use this command to make a hard link between the existing file dihydrogen monoxide.txt and the shortcut name water.txt, txt. A new entry will go into the iode table that points to the same place as the original file. And you'll notice that the number of links for these two files is now two because there are two links that point to the same file. And that's the idea behind hard links. Let's see this in action. Here's our current directory. I'm going to say ls-l and I'm going to add the d- iode option to show the iode number for each file. And you'll notice that each file has a different iode number and they all currently have one link to them. Let's show what's in the file that I want to shortcut. Hydrogen monoxide.txt. And there that is. Now we're going to make the hard link. Just like CP and MV, the original comes first and the shortcut name comes last. >> [snorts] >> Doing ls-li where the i is a short form for the iode option shows that our link count has increased for the original and the shortcut. And you'll also notice that their iode numbers are the same. That means if I cat water.txt, txt I'll get exactly the same as dihydrogen monoxide.txt because it is the same file. I can make another link. Let's link dihydrogen monoxide.txt to H2O.txt. And now when I do a long listing with my i nodes, you'll see that I have three links that all refer to the exact same iode number. Let's clear the screen and again show that information. Now what happens if I remove that original file? The iode entry for the original is gone. But the file will not disappear because there are still two links to the file. Let's try it and see it in action. I'm going to remove dihydrogen monoxide and I'm going to use completion to help me on this one. And now once again long listing with iodes. And you'll see that h2o.txt and water.txt are still there. And now they have only two links instead of three. And again we can access it by either name. We can say cat h2o.txt txt and we can catwater.txt and we'll get the same result because they're at the same position. They have the same iode the same file. When the link count goes to zero when nobody is linked to the data, then the contents will actually be deleted. Let's go and see what we have here. If I remove h2o.txt txt. Now, water.txt has exactly one link to it, and we can still access it. But if I finally remove the last remaining link to that file, it's gone and I can't see it anymore. So, all of this works rather nicely, but there are a couple of problems with hard links. First, you can only link to files, not to directories. I'm going to do a little bit of magic and bring in a directory for us here. And let's see what we have now. I now have a directory. If I try to link my original directory laws of thermodynamics to a shortcut name of thermo, I'll get an error. Hard link not allowed for directory. Another problem is that each disk partition has its own iode table. So you can't link to a file that's on a different partition. For example, if I try to link to a file named slserve/www/ht docsindex.html, which is on a different partition on this machine, and I say, well, let's link this to something called webfile.htm. html in my current directory which is not on the same partition. I get the error message invalid cross device link. In summary, hard links provide a solution. It lets us make shortcut names and our files will go away when the last link to them goes away. The bad news, you can only link from a file to a file and not a directory. and the files must be on the same device or partition. In order to overcome these problems, the symbolic link was created. And that's the topic of our next video.