Create a Symbolic Link in UNIX

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: 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

 

9 Comments -


  1. Nathan Stiles said on January 28, 2009

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

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

  2. Tom said on March 5, 2009

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

    ln ../init.d/httpd S77httpd

  3. Thomas said on March 30, 2009

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

  4. Pranab said on June 3, 2010

    What is the Difference between symlink and hard link

  5. Vick said on October 29, 2010

    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.

  6. Jim said on November 16, 2010

    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.

  7. Jim said on November 16, 2010

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

  8. NN said on December 14, 2010

    Thanks Jim for a very good explanation!

  9. nooby said on September 8, 2011

    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?

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -