Linux Essentials - Symbolic Links

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] hello again everyone and welcome back to my linux essentials series the series where i teach you well the essentials of linux and in today's video i am going to teach you about symbolic links i'll mention hard links as well but we're going to focus on symbolic links it's basically a way that we can link one object to another and we're going to talk about it in this video before we get into the topic at hand today there's a few things that i would like to mention first of all i love making content for you guys i love linux and i love doing these videos but it's a lot of hard work to get these videos out the door so if you are able to support the channel and support my content then i really appreciate it and there's several ways that you can support this channel and one way is you could go on patreon and become a patron of learn linux tv select tiers even get access to videos early before anyone else which is always cool and matthew michael and richard swan are recent examples of patrons that have actually decided to support this channel and i really appreciate that in addition to that i write books in fact right here i have my latest book mastering ubuntu server third edition if you maintain ubuntu servers then it might be worth a look because i go over everything from the initial installation all the way up to maintenance security even how to spin up your very own ubuntu server in the cloud so if you want to support the channel that's another way that you can do so but you don't have to buy anything or pay me money to support this channel the easiest way to support this channel is to share this content with your colleagues because word of mouth really helps and there's one last thing before we dive into symbolic links i'd like to take a moment to mention the sponsor for today's video lenode lenode has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the node's platform you can get your server up and running in minutes and they include all of the popular distributions such as centos debian ubuntu fedora and get this also arch linux and let's be honest what could be better than a linux cloud server provider that allows you to tell all of your friends i run arch lenod has multiple server plans available to make any app scalable and flexible you can use it to host a blog set up a vpn server a minecraft server or you could do what i did and set up a website for your youtube channel because the official website for learn linux tv runs on lenode and lenode offers 24x7 365 support regardless of plan size so you can get live help from a real person when you need it new users can get started right now with 100 in credit towards a new account and i highly recommend you check them out because lenode is awesome thank you so much to lenode for your continued support of learnlinux tv i really appreciate it now without any further ado let's go ahead and get into symbolic links so as i mentioned in the beginning of the video a symbolic link is a way that you can link one object to another but before i show you how to create one and give you an example of why you might want to create a symbolic link it's important to understand the concept of inodes if i use the ls command as you already know we'll get a listing of the objects inside our current working directory on my end i like to use ls-l because that gives me a long listing of the files and directories in my working directory and here you can see that i have a bunch of directories they're the ones in blue and like i mentioned in several videos now even if we didn't have the color we would know which ones are directories and which ones are files based on the first character of the permission string all the way on the left d is for directory and hyphen is for file and that's another reason why i like the dash l option for ls to get a long listing not just because it makes the output easier to read in my opinion but it also allows us to see the permission string that we normally wouldn't see with the output if we didn't include the dash l option but one option i haven't shown you yet is ls dash i i for inodes and the output looks a little strange now because we see again the same directories and the same file but we have numbers to the left of each i can also combine the dash i option with dash l as well i could do ls-li like that and we have some new information here on my end we still have the permission string but it's no longer the first column we have the inode numbers all the way to the left and you can see that they're different for every object so what's an inode an inode is a data object that contains metadata about the files within your file system inodes contain pretty much every piece of information regarding the files and some examples of that include but are not limited to things like the size of the file who owns it the permission string itself and more the actual name of the file itself is not stored in an inode though and the actual content of the file isn't stored there either so basically it's all the metadata other than the name and the content itself another important fact about inodes is that every storage medium has its own set of inodes for example this text file right here has an inode number of 8299 and this file is located in my local file system it's not on a flash drive or external storage device or anything it's here in my home directory now this inode number would have absolutely no meaning whatsoever when it comes to a flash drive if i plug in a flash drive i might very well have an inode number of 8299 on that flash drive and if i do it's just a coincidence because every storage medium has its own set of inodes so basically inodes are a one-to-one match a match to a file every file has an inode every directory has an inode you can see that right here regardless of if the object is a directory or a file it has an inode and like i mentioned these inodes are specific to the file system the storage device the partition so if i was to move this text file to a flash drive it would get a different inode okay if i go any deeper regarding inodes and the internals of the linux file system i'd be out of scope and this would no longer be an essentials video inodes are important to understand at least at a basic level because they come into play when we start dealing with symbolic links or sim links for short later on in this video i'll tell you how inodes relate to sim links but for right now let's get back to the main topic of links now if you have ever used a windows system which is probably most of you then you're probably already familiar with the concept of a link and these are called shortcuts on windows and i have one right here here is a link or a shortcut to steam a popular game distribution platform now the thing is the steam application isn't actually located on my desktop if i check the properties of this we can see that the target is actually in my c drive in program files a directory called steam and then there's an executable program named steem.exe this shortcut is very useful because i don't have to remember this path right here anytime i want to launch steam i just double click on this icon the thing is a shortcut basically a link is a very useful thing because it allows you to create a link to an object that actually exists somewhere else so you don't have to memorize where the original actually is essentially it could give you quick access to something now back on my linux machine we could create a link here as well and for this experiment i have a directory right here called desktop and anything i place inside this desktop directory is going to appear on my actual desktop which is similar to windows in other platforms like windows now we're going to completely ignore the fact that not all linux desktop environments have desktop icons but if you are using a desktop distribution of linux and it's set up to have desktop icons then it's going to work the same way as it does on other platforms now also i have a documents directory and inside there i have two text files i have a sample file called text file and a file called weekly report and these file names include underscores instead of spaces that's not required it's just a habit that a lot of us linux people get into it's just easier to type the paths because you don't have to escape a space and that's beyond the scope of this video i just wanted to mention that as an aside but the thing is i have these two files and what i want to do is create a link to one of these files and i want that link to be on my desktop and the command that we are going to use to create a symbolic link is the ln command and there's actually two types of links that we can create a symbolic link and a hard link i'll let you know what the difference is later on but we're going to use the dash s option here next what i'm going to do is type the path to the file that i want to link to and i'm going to type the entire path and i'll tell you why later so the file is located in my home directory and it's inside the documents directory and i called it weekly report and next what i'm going to do is type the path where i want the link to show up so again that's going to be in my home directory and i want the link to be on my desktop so i'll press enter and you can see that the link showed up right here on my actual desktop it even has a little emblem of a shortcut right there on the icon and that's very similar to windows for example because it also shows a shortcut emblem on its icons as well and if i list the contents of the desktop directory you can see that we have the link right here this particular terminal is giving it a different color and it includes a little arrow here which is pretty cool this arrow points to where the target actually is so we know that this weekly report is a link and one of the ways that we know that is we have an l here at the beginning of the permission string and then of course we see the target so if i go into my desktop directory and that's where i am right now if i cut the contents of the weekly report link i'm actually going to get a listing of mp3s because i used this file in a different video specifically the one where i was talking about the find command and in that video i used the find command to basically search my home directory for mp3s and then redirect the output into a file but the contents of the file the name of the file doesn't really matter the actual file is not located in my desktop directory it's actually located in my documents directory but that doesn't matter because i have a link to it so wherever it is i can refer to it in fact i'll even double click on it i have an icon right here and there it is so as you can see right here on the command line i have a link and that same link is understood by my desktop environment as well and that's one of the things that i like about symbolic links in linux because it doesn't matter if you have a gui basically a desktop environment or not you could be running on a server that has no gui at all and it doesn't matter because links still work in either case in fact i can open up the home directory here on my laptop i can hold down alt hover over the text file and drag it i could drag it into another area but i can't drag it onto the desktop because i'm using the desktop icon extension some of the features are missing normally you would be able to do that but essentially i can hold down alt and drag it with my left mouse button and then let go and then an option link here appears and i have a link to that file now obviously this link is kind of useless because it points to something that's in the same working directory so it's not really all that more convenient but here on the terminal we can see that we have the link right here and even though i created it via my desktop environment the desktop environment created it as a symbolic link so it's the same thing and it could be the case that some desktop environments might do this differently but it doesn't really matter you get the idea symbolic links work the same way in graphical user environments as they do on the command line so you don't have to remember two different technologies for the same use case now let's get back to the concept of inodes because what i need to do now is let you guys know how inodes relate to symbolic links because i did tell you what inodes were at the beginning of this video and i mentioned that they relate to symbolic links in some kind of way and what i'm going to do right now is let you know how exactly they do relate to that so if i use the ls-li command again the dash l is for long listing and the dash i and i combine the two options the dash i is for inodes i want to see the inode numbers and here we do see that now we have the symbolic link right here that's the second one that i've created and it has an inode number of eight three three zero the text file that it links to has an inode number of eight two nine nine so the important takeaway here is that they are not the same inode number they are different and the reason why that is is because the symbolic link itself and the file that it links to are two completely different objects yes the symbolic link links to something but it is not that something it's its own thing which is why it has its own inode number now one thing to understand is that symbolic links are also referred to as soft links and the reason i'm pointing that out is because there's another type of link and it's called a hard link and this also has to do with inodes as well because how they actually refer to inodes is completely different depending on the type of link it is so when i created the most recent symbolic link and here's the command right here i used the dash s option like i mentioned earlier that was how i told the ln command that i wanted to create specifically a symbolic link if i did not include that dash s option then i would have created a hard link instead so what i'm going to do is i'm going to actually remove the dash s and i'll even give the link a different name i'll just call it new underscore link i'll press enter and we have new link right here now notice that there's no shortcut emblem on top of it so we can already see that there's some differences here and since that was created in my desktop directory i'm going to go into that directory and let's list the storage and there we see the new link that i've created and then we also see the original symbolic link that i created earlier in the video now what's interesting is that even though i created a link this is a hard link the first character on the permission string is a hyphen and as you know from earlier videos in the series a hyphen means file so i created a link but showing up as a file that's kind of weird let's investigate a little bit further here i'll add the i option to the ls command and we can see the inode number of the link here has an inode number of seven thousand so if i do the same thing against my documents directory and list the storage of that what's interesting is that the inode number is the same these two objects have the exact same inode number now that's interesting so what does that mean does that mean they are the same object actually yeah that's exactly what it means a hard link is actually a duplicate entry so in that case it now makes sense why they have the same inode number because these two objects are the same now there's some other differences between symbolic links and hard links as well and one example of that is that i would not be able to move the new link the hard link to a flash drive an external hard drive an nfs mount or any other file system because the inode number of 7000 doesn't have the same meaning on other file systems the inode number of 7000 might already be taken maybe it's not maybe that's actually free and open but it doesn't matter because the hard link is a duplicate entry in the file system and that inode number of 7000 will either not exist on the target or it does exist but in either case it's not going to point to the same thing because the inode number of 7000 which exists in that file system or not doesn't have any relation to inode number 7000 on the source file system so i will not be able to move the hard link to another file system at all when it comes to a symbolic link i would be able to move that to a different file system because it is in and of itself a completely different idno number but there's actually another issue with symbolic links as well so what i'm going to do is i'm going to remove the original symbolic link that we've created and you can see on the desktop it's disappeared and then what i'll do is go back to my home directory what i'm going to do is recreate that symbolic link i'm going to do it in a way that's a bit different than how we did it before and this will not work very well just to give you a little spoiler but i'll create the symbolic link and i'm going to create the link to the same file as i did before and the file that i want to link to is in the documents directory it was the weekly report file that one right there and what i want to do is create that back on my desktop again so i'll just use the tilde that shorthand for my home directory i want to create it in my desktop directory and i'm going to name it invalid underscore link because that's exactly what's going to happen it's going to be invalid now it did create that link you can see the icon there on the desktop but instead of having a shortcut emblem it has an x there so something is a little off about this so if i actually list the storage of the desktop directory it's colored red it may or may not be colored red on your side because colorization is a shell customization but the color doesn't really matter although it might make it a bit hard to see in the video but what i'm going to do is use the cat command to cut out the contents of the file now before when we did this it actually showed the contents of the target file the file that the link links to but in this case it's not going to do that no such file or directory so what's the problem well here's the problem i'm currently in my home directory that's my current working directory and then when i ran the most recent command to create that link this was the command right here i created the link to link to the weekly report file that's in my documents directory and notice i did not include the full path to the documents directory i included a relative path so i didn't put anything before it i left out the slash home slash j that should have been in the beginning so the file i wanted to link to was in my present working directory in my documents directory called weeklyreport.txt and then i created the link in my desktop directory and i named it invalid link now this would have actually worked if i did not save it in my desktop so what i'll do is actually remove this part right here i'll press enter invalid link is actually a valid link and to prove it it works now the reason why this worked just fine is because what i did was i created the link right here in my home directory and that allows the link to function just fine because it refers to a documents directory and inside there a file named weekly report which is true because i am currently in that directory i am in my home directory and there is a documents directory here and there is a file there named weeklyreport.txt but when i created that link in my desktop that fact is no longer true there's no documents directory in my desktop so the link can't possibly work because it's referring to a path that doesn't technically exist in that current working directory if my current working directory was my desktop and to illustrate that we have the invalid link it links to a documents directory and then the file name but of course there's no documents directory in my desktop so that is a weakness of the symbolic link and that wouldn't have been a problem if i included the full path so if i delete that link i'll recall the command and what i'll do is i'll type out the full path to my home directory right there in front of that path right there and i will recreate that in my desktop directory and i should probably delete the one that's right here first and it's gone so i'll press enter now notice that invalid link has the shortcut emblem on it instead of the x and it works and that's because i included the full path so it doesn't matter where i move this to the path is there it's in the link it's the full path so i can even just cut this right here go to my home directory i'll paste it right here and it still works it's green and it's referring to a path that actually does exist now a hard link is a little bit different than that so what i'm going to do is remove that invalid link and i'll also remove this one as well just get this cleaned up a little bit i'm going to create a brand new hard link so i'll use the ln command i'm not going to include dash s this time and then i'll type the path where i want to link to so what i'll do is i will type the path of the documents directory and then the same file the same one that we've been using and i'm going to store that in the desktop directory and i'll just call it new hl short for hard link i think that works i'll press enter and there we have the new hard link now notice that i used a relative path here i did not type the full path it still works why does it still work well that's one of the ways that hard links differentiate itself from symbolic links since hard links are a duplicate object it's actually matched one to one to that file regardless of where the link or that file is in fact what i can also do is i can go in here to documents there's the weekly report file i'm going to rename it i give it a completely different name and that's going to break any sim links but it's not going to break a hard link regardless of the name it's path it's a one-to-one relationship that hard link is a duplicate entry of the file that i created the link to so you might be thinking then that hard links are actually superior to symbolic links but not so fast what i won't be able to do is move that hard link to a different storage medium because the inode entry is going to be different on the target storage medium so the inode number is 7000 which you can see right there but if i plugged in a flash drive for example that inode number will have no meaning whatsoever symbolic links are able to cross file systems i can actually link to a file on a different file system so for example if i have an nfs share mounted i can actually create a symbolic link to a file in that share in my home directory and it doesn't matter they can cross file systems now with a hard link i wouldn't be able to do that not only that when we use the ls command we get more information when it's a symbolic link it's more obvious you have the l at the beginning of the permission string and the output even shows you where that link links to if we only used hard links then a system administrator that is looking at the server they may not immediately understand that that hard link is a link you and i know that the links that we created when we didn't include the dash s option were hard links but if someone was looking at my computer after i left and i didn't tell them that they may not be immediately aware of that so symbolic links are usually the way to go most of the time so at this point i bet you are just itching to create symbolic links all over your server your desktop you just want to create a bunch of them because they're extremely convenient and they're awesome but not so fast symbolic links are something that you should only use if it's absolutely necessary because if you are linking things to other things you know quite often then a system administrator will basically have to follow all the links and their job becomes harder so symbolic links are one of those things that you should use as a last resort when you basically have no other option now on a desktop nobody cares that you have you know 10 000 shortcuts in your home directory to all your favorite files that's totally fine but on a server you know you could get into a situation where you have things linking to other things and that can be confusing and annoying to other administrators so i wouldn't get into the habit of creating sim links everywhere and in some cases you can actually start breaking things especially if you are linking to binary files that would otherwise be supplied by packages for example so if you install a package it has some binaries and then you include a bunch of links everywhere it just gets confusing so i wouldn't do that but symbolic links are a tool that you can use when it makes sense to use them so just keep that in mind now you know how to use them you can add this to your list of tools that you know how to use and that's the important point you will run into situations when a symbolic link makes sense but just don't go crazy with it and you'll be fine so i hope you enjoyed this video let me know in the comments below if you have any thoughts or suggestions subscribe if you haven't already done so and click that like button if you like this video that lets youtube know that you want to see more content just like this thanks for watching [Music] [Music] you
Info
Channel: LearnLinuxTV
Views: 9,385
Rating: 4.9896908 out of 5
Keywords: Linux, LearnLinuxTV, LearnLinux.tv, symlink, symbolic links, ln command, sym link, soft link, linux link, linux links, symbolic link, hard links, linux tutorial 2021, hard vs symbolic links linux, linux tutorial for beginners, linux for beginners 2021, linux commands, symbolic, symbolic links in linux, links in linux, create a symbolic link in linux, hard and soft links in linux, soft and hard links in linux, linux symbolic links explained, hard symbolic links in command line
Id: zfSa-PEU3h4
Channel Id: undefined
Length: 30min 6sec (1806 seconds)
Published: Tue May 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.