How to: Linux / UNIX create soft link with ln command

Q. How do I create a soft link (symbolic link) under UNIX or Linux operating system?

A. To make links between files you need to use ln command. A symbolic link (also known as a soft link or symlink) consists of a special type of file that serves as a reference to another file or directory. Unix/Linux like operating systems often uses symbolic links.

Two types of links

There are two types of links

  • symbolic links: Refer to a symbolic path indicating the abstract location of another file
  • hard links : Refer to the specific location of physical data.

How do I create soft link / symbolic link?

To create a symbolic link in Unix or Linux, at the shell prompt, enter the following command:
ln -s {target-filename} {symbolic-filename}

For example create softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:
ln -s /webroot/home/httpd/test.com/index.php /home/vivek/index.php
ls -l

Output:

lrwxrwxrwx 1 vivek  vivek    16 2007-09-25 22:53 index.php -> /webroot/home/httpd/test.com/index.php
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 7 comments… read them below or add one }

1 Dominic 06.13.08 at 11:22 am

I executed this comment successfully and created a soft link. When I tried browsing to “/home/vivek.index.php” it shows “403 Forbidden error is thrown. Is there is any possibility to browse on the same?

2 Anil Kumar Jena 06.23.08 at 11:03 pm

This error 403 access forbidden, means that a request for a “bare” directory path has been made, no default directory index page is present and the site manager does not want a file listing displayed in its place.
If you have problems accessing a particular file or directory, ensure that the Web server has permission to read those files. Usually, the Web server will operate under a special user account, so make sure that user has read permission.

In this case, either create an index for that directory or change the settings to permit directory listings. For Apache, use the Options +Indexes directive in the .htaccess file for the directory.

3 asif 10.04.08 at 2:06 pm

how do you create a symbolic link to directories…

4 Vishal 12.12.08 at 5:59 am

I want to know that how to preserve these softlinks while copying the files on Solaris???

5 samir 12.22.08 at 5:05 am

Hi All
If any one saw my msz.
Write me the advantage of soft link(symbolic link) only.

6 anusha 02.10.09 at 8:15 am

How can we know about the various options under link command.

7 a11an 06.15.09 at 11:46 am

Hi,

Wanted to confirm the information I had. Great info, it worked.

Created a soft link for apache-tomcat

Syntax:

Soft Link
ln -s /directory/of/application/ /directory/and/softlink

e.g. ln -s /usr/local/apache-tomcat /usr/local/apache
This created an ‘apache’ softlink to apache-tomcat directory

ls -l
apache -> /usr/local/apache-tomcat

To use hard link, just remove the -s option

Hope this helps.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , ,

Previous post: /etc/network/interfaces Ubuntu Linux networking example

Next post: Block ip address of spammers with iptables under Linux