Creating Symbolic Links on Command Line

Video loading...

  • 0:03
    Creating Symbolic Links on Command Line with Addison Berry
  • 0:08
    Hey, there.
  • 0:09
    In this command line basics video,
  • 0:10
    we're going to be looking at symbolic links,
  • 0:13
    which are also referred to as sym links in the lingo of
  • 0:16
    the cool kids. And basically what a symbolic link is,
  • 0:20
    it's a shortcut.
  • 0:21
    So, if you want to have something in one folder that
  • 0:27
    actually points off to some other files or folders that
  • 0:30
    are located elsewhere on the machine,
  • 0:31
    that's essentially what a symbolic link is
  • 0:34
    going to do for us.
  • 0:35
    So, let's take a look and see how that actually works.
  • 0:39
    So I'm going to start off in my home directory here and just
  • 0:41
    look at an existing sym link so that we can
  • 0:44
    just sort of see where that is before we dive in.
  • 0:47
    So I'm going to do an ls-al on my home directory
  • 0:51
    so I get a detailed listing,
  • 0:53
    and you'll see that the X-Chat Aqua application
  • 0:57
    I have has added a link in my home folder here.
  • 1:00
    And you'll notice it begins with an l for link
  • 1:04
    as opposed to d for directory, which,
  • 1:07
    like down here, see that's a folder
  • 1:10
    rather than a link.
  • 1:12
    This is the file name for what actually appears when
  • 1:15
    I look at my files, and then this arrow is saying, hey,
  • 1:19
    I'm actually pointing elsewhere, and then it has the full path
  • 1:22
    to that other location.
  • 1:23
    So, this application is sticking stuff in the application
  • 1:27
    support folder but referring to it from my home directory.
  • 1:30
    The use case that I want to look at in this
  • 1:32
    video actually has to do with websites and being
  • 1:35
    able to let your web server know that you want
  • 1:38
    to serve up a site from another location rather than
  • 1:40
    just the document root.
  • 1:42
    So I'm going to go into my sites directory.
  • 1:44
    Sites is my local host, and these are all the different
  • 1:47
    websites that I have installed on local host.
  • 1:49
    If I go into my browser here, you'll see here's local host,
  • 1:53
    here's a list of all those websites.
  • 1:55
    So this is my local host on my local machine here.
  • 1:58
    And what I want to do is be able to put other,
  • 2:03
    refer to other websites from local host without
  • 2:08
    necessarily having to put the code here.
  • 2:10
    So I'm going to show you.
  • 2:12
    I have, for do it with Drupal, we have an SGN checkout
  • 2:15
    of all the code for that, and so it's in a totally
  • 2:19
    different location than sites.
  • 2:20
    So I'm going to scroll through and actually get down to
  • 2:24
    where the Drupal site is that I want to refer to here.
  • 2:29
  • 2:30
    Alright, now, so this is the root of my Drupal site,
  • 2:34
    and I want to be able to access this from local host.
  • 2:36
    I don't want to have to go to this whole path.
  • 2:39
    This is my present working directory.
  • 2:42
    My web server's not serving me files from here,
  • 2:47
    so that's not very handy.
  • 2:49
    I want to get to it from local host.
  • 2:51
    What I want is, I want to get this entire path.
  • 2:54
    I need to know this entire path so that I can basically
  • 2:57
    point to this from my sites directory and kind
  • 3:00
    of trick my web server. So I'm just going to do a
  • 3:03
    CTRL+C and actually just copy this entire path
  • 3:05
    so I don't have to type it in again later.
  • 3:08
    But all I need is the path, so you can either type it
  • 3:10
    in manually or copy and paste it, however.
  • 3:12
    I'm going back to my sites directory.
  • 3:15
    This is where my websites are listed.
  • 3:17
    The code for this particular website is in that completely
  • 3:19
    different place, so I want to create a link to point to it.
  • 3:25
    And the ln command is link. So, to create a link,
  • 3:30
    it's just ln. We need to add a few things after that.
  • 3:35
    We want to do a symbolic link.
  • 3:36
    There's also something called a hard link,
  • 3:38
    but we want to use symbolic links as a simple, light pointer
  • 3:42
    rather than copying and making copies of things.
  • 3:45
    And then I'm going to paste in that path where the code
  • 3:48
    is that I actually want to use.
  • 3:51
    So I'm doing a symbolic link here in sites directory,
  • 3:55
    and I want it to point to that.
  • 3:58
    So my HTML folder that's all the way down there
  • 4:03
    in my lullabot directory.
  • 4:05
    And that's pretty much it for creating a link.
  • 4:09
    It's just that, and then you'll notice
  • 4:14
    the name of my folder in the lullabot site at HTML,
  • 4:18
    when I do this command, it just creates a folder
  • 4:20
    called html for me.
  • 4:22
    It just takes the name of the folder that's already
  • 4:25
    out there and just uses that.
  • 4:27
    Now, I don't really want that in my sites.
  • 4:29
    HTML is pretty generic.
  • 4:31
    What site is that?
  • 4:32
    I could do move, which we've looked at in other videos,
  • 4:34
    and change the name of it, but I want to show you another
  • 4:37
    way you can do this from the link command,
  • 4:39
    so I'm just going to remove the HTML link here.
  • 4:43
    Notice I don't have to do a -r recursive.
  • 4:46
    This HTML thing that's here is not a folder,
  • 4:49
    it's not a directory; it's a link.
  • 4:51
    So I can just remove it without having to recursively remove.
  • 4:54
    I'm not removing the folder contents that exist over in
  • 4:57
    the lullabot folder.
  • 4:59
    Okay, so that's gone now.
  • 5:01
    So I have removed that.
  • 5:04
    I'm hitting the up arrow to go back through my commands,
  • 5:08
    and I get back to this link command, so we'll review it.
  • 5:11
    ln is for link. The -s means symbolic. And then
  • 5:16
    this is the path of the code I want to use.
  • 5:18
    And now I can actually just type a different name in here.
  • 5:21
    If I don't want to use that folder or file name,
  • 5:24
    I can type my own custom one, so I'm going to type in diwd,
  • 5:28
    because that makes more sense to me.
  • 5:30
    And now when I run the command, you'll see instead of HTML,
  • 5:34
    I have a diwd quote-unquote folder, which is actually just a link.
  • 5:39
    If we do a detailed listing here,
  • 5:41
    you'll see this is actually a symbolic link and not a folder.
  • 5:46
    It's just peering into a folder that exists elsewhere.
  • 5:49
    But, if I change directory into that folder, into that link,
  • 5:54
    I can see everything just like it is in the other directory.
  • 5:58
    And if I go back to my local host and reload,
  • 6:01
    you'll see I have a link to diwd. And when I click on it,
  • 6:05
    it takes me to the Drupal site using that code all
  • 6:08
    the way over in the lullabot directory.
  • 6:10
    And now I can go ahead and install the site and do what
  • 6:13
    I need to and still access everything from local host.
  • 6:16
    It's really handy, and that way I can keep my code organized
  • 6:20
    where I want it, the way I want it because I'm kind
  • 6:22
    of particular about how I organize my directories.
  • 6:26
    So, for a real quick review:
  • 6:27
    Link, you want the -s for symbolic,
  • 6:30
    we want the full path to what it is that the shortcut
  • 6:33
    is pointing to, and then optionally, at the end,
  • 6:35
    you could change the name of that shortcut,
  • 6:38
    but it's not required.
  • 6:39
    The only thing that's really required is that path
  • 6:42
    to what it's pointing to.
  • 6:43
    So, have fun with sym links.
  • 6:46
    They're really, really handy.
Loading ...

Creating Symbolic Links on Command Line


This video shows how to use the magical symbolic link, or symlink. These are basically a handy *nix way to create shortcuts. They come in particularly handy if you want to organize code for your websites outside of the web server's document root and that is the example we use here.

Note: this video was originally released November 23, 2009 on

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.