Hard and Soft Links in Linux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video lecture we're going to talk about the concept of links in Linux we'll take a look at hard links and soft links and talk a little bit about how they're implemented under the hood so first of all let's start thinking about how your hard drive is organized and talked about a concept called inodes basically on a UNIX system if we think of the hard drive that our operating system installed upon you can kind of think of there well you can think of there being three distinct areas on that hard drive one of those areas is called the super block super block contains information about the type of the file system its size and some other information about the status of viol system welcome to talk about super bucks too much today but the really important they're actually stored redundantly on the disk in case there's ever a problem with one of them there's backups because it's that critical to how the disk operates within the context of the operating system next up is this concept of the inode table and this is the concept that's most relevant to our discussion today in Linux inodes contain as the slide says here information about the files on your system specifically it talks about their mode so we do like a chmod you can see information about that stuff comes from the inode table other information like who owns at what size it is creation of modification dates and then more importantly at the bottom in addition to keeping all of this metadata related to the file itself it keeps information about where one can find the actual data on disk and it's kind of important to note that an inode table is actually a reference to where real data is actually stored out in the data blocks so when you store data it's actually being stored out in the data blocks just kind of wherever there's available space but what ties that data that's stored out the data blocks back to the the operating system is this every file has a reference in the inode table that points to data out in the data blocks if you've ever seen data recovery software where at some point you have deleted say a picture and then you actually ran a recovery program to get that picture back the way it works in most cases is that when you actually delete something from a hard drive it doesn't get deleted from the data block area the data just gets unlinked from the inode table so you lose the you delete the reference in the inode table but the old data just stays out in the data blocks until it's overwritten by newer data so that's why in some cases data can be recovered even after you've deleted it as long as you haven't done a lot of writing data to the disk after that point anyway the key here today is going to talk about eye notes and the inode value is going to be useful to us and discussing what links are so let's think back to when we were using LS dash L output and talking about each component and specifically I want to look at the area of LS dash L output called the war which is the links area and so this basically gives the total number of links to a given file and this number is going to be important to us as we discuss this concept so keep this column or well this is the other specific column of information in mind as we start talking about this bit of information so before we look at this on the command line what are links what are hard versus soft links well to simplify this soft links are basically just like shortcuts in Microsoft Windows you may have shortcuts on your desktop to programs in another folder and a shortcut is really just a little file that tells you where to find another file so this idea of shortcuts probably shouldn't be new to you we'll actually look at this and take a peek at how it works within Unix the concept that's usually new to people is this idea of hard links and the simplest way to think of hard links is that they're just a different name for the same file so as we're in a shortcut situation you have this new file that points to a file when you have a hard link you're really just creating a new file name that links to the original file and so we'll take a look at what this means on the command line and we'll talk about how this works in terms of creating these and so let's go look at the command to create one of these and then talk about them I'll start with shortcut soft links first because they're the easiest alright so actually before we go the fan line let's take a look at what a soft link is soft links when I create a soft link to a file when I create a shortcut to a file I'm going to create a new file and that file is going to point to an original file okay and what you'll notice is that each one of these shortcut links will be given its own inode value so we'll know that these are actually different files and we'll look at how to view a files inode number in a second but basically if the inode value is different then you know your file is different if the inode value for a file is the same then you know that that file is the same and we'll see this again in the second on the command line so the way we create soft links is we use the Ln command with the - s option and then what we'll do is we will put our file that we want to make a link to and then finally the name of the shortcut we want to make that link to so let's go look at this on the command line and then we can come back to this slide if necessary to discuss so in my current directory I have a file called test that I've used in some previous demonstration so let's go ahead and use that so if I make a shortcut to this well first of all let's take a look at some information about the file test and specifically I'm going to use LS - elbow gonna put the I option in here which will show me the inode for that file and so the first value that we see is one hundred thirty four thousand six hundred and seventy one this value is that files inode it's like it's address on disk this is the unique identifier in the inode table that contains all of this information about the file as well as the pointer out to the actual data in your hard drives data block so what's interesting here is that you'll notice that this file has a single link and we're talking about links today so great so this file has one name so let's create a soft link to it so I'm going to go ahead and I'm going to create using the Ln command with the - s option a new link and I'm going to point that link to the test file and I'm going to call it my shortcut shortcut okay now if I do an LS you'll notice that there is a new file in this directory notice that that new file is also a kind of like a teal blue and that it appears a little bit differently so let's take a look at just using LS l I the test file and the my shortcut file so we can see how they differ so the first thing you'll notice that's a little bit different here is that if we look all the way to the right you'll see my shortcut and then something that looks like an arrow and then the name test and what this indicates is that this is actually a shortcut file or a soft link that points back to the filed test if we go back all the way to the left you'll notice that the soft link I just created has a new inode number 133 966 and it's different than the filed test so what you can tell from the inode values as well is that these are two different files my shortcut is a special file and test is its own standalone file both these are standalone files the only difference is that test contains data and my shortcut just points to that original file another way to know that this is a shortcut is if you look the first letter in front of the permissions for the file is an L which stands for link and on the original file notice that it stands for just it's just a - for a regular file so if I look at what's in the test file you'll notice that it has some words was thinking about using this for possibly a grep exercise and what you'll notice is if I just cat test I get the contents of that file what happens if I cat my shortcut I get the exact same answer why well my shortcut is a is a link that points to the original file test let's clear this real quick and let's bring back that LS - Li of both of these files now what happens if I delete the test file because shortcut points to test well if I delete the test file that means shortcuts now no longer pointing to a file so you know better yet instead of deleting it I'll just move it to a new name and now if I do an LS dash L you'll notice I get an error because and I've been helped if I worked on that and you'll notice now this turns out to be red and there's a problem there because this now says it was looking to point to a file called test and if I try to cat my shortcut I get an error that says no such file or directory because my shortcut was pointing to a file named test and it or if I deleted it I'd get the same answer but if I look at renamed test the files still there all the data is still there so again this is just a simple way to create shortcuts let's take a look at this and see what the GUI makes this look like I'm currently in my home directory I am going to go into my desktop directory which these are the directories that I utilize for you know putting stuff on the Ubuntu desktop and you'll notice that already have a link in here to terminal so if I do an LS dash L I on terminal alright what you'll notice is that this is a link because it starts with L it's also a teal blue I'll notice that it has the inode of one three seven one five three and you'll notice that this link actually points to a file called slash user slash bin slash gnome terminal and why do I have this shortcut here well here's why I'm going to minimize the console and I'm going to take a look at my a bun - desktop which is hiding some things here because you'll notice that on my desktop I actually have a shortcut now this looks like Windows right you see the little arrow that points up to the file and you see that it says terminal and actually if I were to right-click on this or see if this works properties will even show me that this points to this file that it's a link so this is probably the view most of you are used to seeing shortcuts in because this is very similar to how Windows deals with links so that's the concept behind shortcuts or soft links and the proper term here soft links you know it's important that you know on tests and whatnot that we're dealing with this idea of soft links so next up we'll take a look at hard links hard links are a little bit more difficult to understand because they're different from the shortcut links or the soft links that we're used to dealing with in Windows but to simplify it once you get started the easiest way to think of a hard link is just another name for a file so this idea when we create a hard link to a file we're really just creating another name that points to that file and we could actually put that hard link in any directory so what's really nice is you could have a single file exists on the system and then you could just have a bunch of names that point to that single file and once you'll notice is that all of the names when we do an LS dash L I on them will have the same inode because they all point to the same file and the command we're going to use for this is Ln without the - s option so just straight Ln and this is called hard linking so this slide demonstrates this concept by showing you that there's three different names that point to the same file and we'll actually be able to see how many links point to a given file by looking at the link column in the LS dash L output so let's take a look at the shortcut that was on my desktop that pointed to the terminal and so what you'll notice here is that this is a shortcut link right so this again has this idea that there is some type of you know separate file pointing to the gnome terminal file executable on my system and if I were to do an LS dash L I on the gnome terminal you'll notice that it has a separate inode right it's actually owned by root I'm allowed to run it and this idea that these are separate files because again these have separate inodes so just enforcing the idea of soft links before we get into hard links so let's start to create some hard links to let's say this lab 3 test file so what do I got on that lab 3 test file already well here's a tie note and you'll notice in this column it has one link that means this file only has one name so I'm going to make a new file a name the points don't we make a new hard link to this file so I'm going to use Ln and I'm going to do lab 3 test.txt and I'm going to be like I'm going to call it hard link 1 now if I look at LS - Li on the original file you'll notice that the number has gone from 1 to 2 because now there is a second link that points to that file in fact it's because I created this file called hard link so let's look at both of those files at the same time so what you'll notice is now both of these files have the exact same inode because they are the same file and what you'll notice is that both of these have two links and actually when you remove a file like when I delete a file it let me remove the original lab 3 file and we'll go up and we will just do an ally of hard link 1 what you'll notice is now I've deleted the lab 3 test a txt file and now the hard link file remains and it has one link the only way to delete a file in a Linux system is to make sure it has zero links there's actually a find command that you can use that will search your system and make sure that there are no files with a given inode so the idea being that if you see that a file has two links and you're like well where is that second link coming from you can actually use the find name to find files by inode which would then tell you where that other linked file and actually if you do something like um if I look at the directory that I'm in ok what directory am I currently in well I'm currently in my desktop directory and I did an LD and D is a way to just get directory information without getting information about what's in the directory remember now notice that this directory that I'm in has three links to it and so you wonder well ok why does this directory have three links to it well there's a couple reasons the directory has a name and the directory also has a couple of other items to it if we look at whoops sorry let's clear - LS - LA on the current directory notice that dot is a directory for the current is a link to the current directory notice that this director has a name that's a second link and then finally notice there's a directory called top inside of this directory if I do an LS - la on top now notice that top has a dot dot directory well let me throw an inode number counting here let's look at this a little bit better so let's do LS - la eye on this directory so notice that the current directories inode is one three six nine eight seven okay so this directory has three links because it's known as its existing name therefore dot and if I do an LS - la on top you'll notice that dot dot in this directory also points to one three six nine eight seven because that is the name of the directory and then the file also has its own name itself which is the third link so if I were to create like delete top and then do an LS only I have this directory notice that the directory now goes down to two links well what are the two links well the two things that this directory has linked to it is currently its name and the link dot so every directory always has at least two links to it because that directory is known as dot and that directory is also known by its given name one final note about hard links why you can make soft links to directories I just wanted to emphasize the fact that you cannot make hard links on most modern operating systems to directories because it can cause some weird issues with recursion so I think I wanted to make a second take a second to just stress that so if I try to take this directory testing that I've created and create a hard link to it all right and I'll say my hard linked directory I get an error that says hard links not allowed for directory so just be aware that while the system uses some hard links for the dot and dot dot directories that you as a user are most likely unable to create hard links to directories which can create them to files
Info
Channel: Jason Wertz
Views: 95,589
Rating: undefined out of 5
Keywords: Linux
Id: kYonC93SvpE
Channel Id: undefined
Length: 18min 7sec (1087 seconds)
Published: Sun Sep 15 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.