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.

  • Nathan Stiles

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

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

    • Tom

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

      ln ../init.d/httpd S77httpd

  • http://dxml.dk Thomas

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

  • Pranab

    What is the Difference between symlink and hard link

  • http://twitter.com/volatileacid Vick

    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.

    • Jim

      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.

      • NN

        Thanks Jim for a very good explanation!

    • Jim

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

  • nooby

    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?