Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In Unix we are going to want to make use of links. And the first kind of links that we are going to look at in Unix are hard links. The way that we make a hard link is just simply to have ln, short for link, space and then the file that we want to link. That could either just be a file name or could be a full path to a file, if the file is located somewhere else. Followed by a space and then followed by the name of the hard link or if we want to put that link somewhere else, the path where we want to locate it, as if we were creating a file.
But if we are in a single directory, than we would just put the file that we want to link and then the hard link that we want to make to that file. What this will do is make a reference to a file in the file system, the same way that making a Finder alias would do. And like the Finder, it will not break if the file is moved. But there's one important difference about the way that hard links work. Remember in the Finder, if we threw away the original file, the alias still is there, but it's broken. It doesn't point anything anymore. It was just an alias to that original file.
Well, that's not the case with hard links. With hard links, they don't break if the file is deleted. Now that could be a little weird to work with it first, until you get used to it and understand what they are really doing. But when you think about it, every time we see a file listed for us, all it is is a reference to a file in the file system. So making a hard link is really no different. It's just making another reference to that same file in this file system. Both file names that we see on the screen are pointing at the same spot on our hard drive at that same file storage space.
So what we are doing with the hard link is allowing two different names to point to that same thing. That's different than the way the Mac OS X Finder works. The Mac OS X Finder there's definitely an actual file that points to that space on the hard drive and then there's this other thing called an alias which points to that original file, not to the spot on the hard drive but to that other file. Let's try that out in Unix so you can get the hang of it. So notice that here I am inside my Unix files,and in the last movie, ls -la, you can see that I have my linkedfile.txt that I'll be linking to and then I also had these other aliases here.
Now these aliases are not included in the exercise files because they don't travel well. They really are designed for making references on my hard drive, so you won't find them there. But it's easy enough to create these aliases in the Finder. What we want to focus on though is this linkedfile.txt and so we want to make a hard link to it. Well, we use ln and then a space and then the name of the file we want to link. So linkedfile.txt followed by a space and then the name of the link that we want to give it. I am going to call this one hardlink. So it's always in that format: the link, followed by the source, the thing we are targeting, and then the name of the link is the last item.
Now let's do ls -la again, and let's take a look at what's there now. So here's my hardline. Notice that the size of this is much, much more smaller than the size of the aliases I created. It is exactly the same size as linkedfile.txt and that makes sense. These are two files pointing at the same spot on the hard drive. And if we open it up, let's do cat hardlink, now you see it has the exact same text inside of it. Let's take a look over here in the Finder and just see it here. In the Finder it didn't' give it any kind of icon here because it didn't have .txt or anything after it, so it just gave it a generic file icon.
But you can see that it does not have the little arrow that the Finder uses to indicate the special alias files that it creates. So as far as the Finder is concerned, this is just another file. Let's create another directory that we can move this into. I'll make a directory. I'll just call it linkdir and then let's move the original file, linkedfile.txt, let's move that file into linkdir. So now we have ls -la, we can see we have our hardline, and then we have linkdir and if we look inside linkdir, you can see that we have our linked file.
Now, let's try opening up that hardlink again. So same thing, hardlink, yup! It's still got that same data on it. It was able to still keep track of it, even though we moved it. That's to be expected. Here's the weird part. If we delete on one of these, and it doesn't matter which one we delete, if we delete hardlink or we delete linkedfile.txt, the other one will still contain the data. It will not remove it from our hard drive. Let's try that. What I am going to do is I am going to remove the original. I'll do rm inside linkdir and we'll get rid of linkedfile.txt.
So now, the file is removed. Let's just take a look there and see, yup, it's actually gone. Now let's take a look at our hardlink again. See, it still contains that information, even though we threw away the original. The hardlink is exactly equivalent to that file. So there are times when having a hardlink can be useful. But it can be a little bit weird, because you can think that you are throwing away a file, but actually that file continues to exist. And in a shared Unix environment, that can be useful because several users could all have a hardlink to a file and if some makes a change to the file, the change happens for all users. They share this file.
But if one of them throws it away, well, it doesn't throw it away for everyone else. They still have access to it. So in that context it does make sense. But because of how we are use to working with Apple aliases or Window shortcuts, it feels a bit odd.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97381 Viewers
61 Video lessons · 84625 Viewers
71 Video lessons · 68826 Viewers
56 Video lessons · 101292 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.