Create a Symbolic Link in UNIX

A symbolic link is a pointer to another file or directory. It can be used just like the original file or directory. A symbolic link appears in a long listing (ls -l) with a reference to the original file/directory. A symbolic link, as opposed to a hard link, is required when linking from one filesystem to another and can be used within a filesystem as well.


To create a symbolic link, the syntax of the command is similar to a copy or move command: existing file first, destination file second. For example, to link the directory /export/space/common/archive to /archive for easy access, use:

ln -s /export/space/common/archive /archive

To link the runtime control script /etc/init.d/httpd to /etc/rc2.d/S77httpd, use:

cd /etc/rc2.d
ln -s ../init.d/httpd S77httpd

See also: Create a hard link in UNIX

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

9 Responses to “Create a Symbolic Link in UNIX”

  1. January 28, 2009 at 7:15 pm, Nathan Stiles said:

    cd /etc/rc2.d
    ln -s ../init.d/httpd S77httpd

    All rc links should be hardlinks. This is not a good example.

    Reply

    • March 05, 2009 at 10:20 pm, Tom said:

      Maybe you could provide the command syntax to make it a hard link:

      ln ../init.d/httpd S77httpd

      Reply

  2. March 30, 2009 at 4:53 pm, Thomas said:

    > ln -s /export/space/common/archive /archive
    Just what i was looking for. Thanks!

    Reply

  3. June 03, 2010 at 2:15 pm, Pranab said:

    What is the Difference between symlink and hard link

    Reply

  4. October 29, 2010 at 10:05 am, Vick said:

    A hard link is another name for an existing file; there is no difference between the link and the original file. So if you make a hard link from file `foo’ to file `bar’, and then remove file `bar’, file `foo’ is also removed. Each file has at least one hard link, which is the original file name itself. Directories always have at least two hard links–the directory name itself (which appears in its parent directory) and the special file `.’ inside the directory. Likewise, when you make a new subdirectory, the parent directory gains a new hard link for the special file `..’ inside the new subdirectory.

    Reply

    • November 16, 2010 at 3:33 pm, Jim said:

      Vick: Sorry, but that isn’t correct. If you have a file called ‘new’, for instance, and create a hard link called ‘new2′, both ‘new’ and ‘new2′ point to the same physical location on the hard drive. If you do an ‘ls -l new’ you will see something like:

      -rw-r–r– 2 new

      That *2* is the number of hard links to the file. Every file upon creation has one hard link to the location itself, which is why you normally see ’1′ in this spot for files.

      Now, to get back to what I was explaining, if you have ‘new’ and ‘new2′ which is a hard link to ‘new’, and you delete ‘new’, the file is not gone. ‘new2′ still points to that file and is completely usable. Other hard links are not ‘magically’ deleted…and that is the value in a hard link.

      If ‘new2′ was a soft link, however, to ‘new’, and you deleted ‘new’, then ‘new2′ would still exist, but would not be valid any longer, since the file ‘new’ that it points to no longer exists.

      Hope this helps.

      Reply

      • December 14, 2010 at 5:16 pm, NN said:

        Thanks Jim for a very good explanation!

        Reply

    • November 16, 2010 at 3:35 pm, Jim said:

      Oh…sorry…one last thing…a file is not actually deleted unless its hard link count is 1. *THEN* it is gone.

      Reply

  5. September 08, 2011 at 1:22 am, nooby said:

    Hallo,
    indeed I need some help! i’m absolutly new at linux! but I have to administrate a internalwebserver, which is running with Joomla 1.5. We had a symlink to a share on an other Server, but it doesn’t work now. I tried to link again with:

    sudo ln -s \\server\share /var/www/joomla15/slw/

    when I do this I get back, that the file does exist. But the hp the link doesn’t work! so awhat can I do?

    Reply

Leave a Reply