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

by Vivek Gite on September 25, 2007 · 31 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 31 comments… read them below or add one }

1 Dominic June 13, 2008

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?

Reply

2 Anil Kumar Jena June 23, 2008

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.

Reply

3 asif October 4, 2008

how do you create a symbolic link to directories…

Reply

4 santosh July 22, 2010

u cannot create symbolic link to directories.

Reply

5 bbithead September 15, 2010

sure you can:
ln -s /existing/directory /sym/link/to/that/dir

Reply

6 Vishal December 12, 2008

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

Reply

7 samir December 22, 2008

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

Reply

8 anusha February 10, 2009

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

Reply

9 a11an June 15, 2009

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.

Reply

10 Debashis Mohapatra July 24, 2009

Hi,

Pla tell me ,in what scenario,we can use soft link and hard link.

Reply

11 jesse July 24, 2009

“Pla tell me ,in what scenario,we can use soft link and hard link.”

I am trying to use a symbolic link (soft link) to allow multiple websites to share a css style that is beyond each sites root directory.

Reply

12 varunreddy August 28, 2009

how can we send our file information to another user using ln command

Reply

13 Deandre October 13, 2009

Create an additional name (hard link) for the file ‘cars’. The link should
be called ‘autos’ and should be in your home directory:

Reply

14 Deandre October 13, 2009

How do you do that??

Reply

15 Hi SUNIL October 22, 2009

whats the advantage of soft link over Hard link?

Reply

16 Hi SUNIL(IEET BADDI) October 22, 2009

How we can make more the Effective the Hardlink And Softlink?

Reply

17 darshan October 26, 2009

My soft links are not visible through http:
how do I make sure that web server has access to those folder.
Please give the exact set of commands.

Thanks

Reply

18 chepas January 3, 2010


darshan 10.26.09 at 2:47 am
My soft links are not visible through http:
how do I make sure that web server has access to those folder.
Please give the exact set of commands.

that depends on your webserver, for apache:


Options Indexes FollowSymLinks

Reply

19 links management October 3, 2010

You can use .htaccess rules to create it

Reply

20 Devadas April 4, 2011

hi all..pls solve this issue…
why my link size immediately goes to zerosize occasionally?

Reply

21 nikesh June 13, 2011

Please make sure that the file which you are accessing is having a read access for web server.

If you are accessing a file under the home directory of a user there might not be read access for home directorates.

means /home/user folder will be with 700 (drwx——) permission. So you would need to set 755 permission for /home/user folder if you need to access the file through URL.

Reply

22 Ndamu July 5, 2011

Please help me,when i create symbolic links they always come up broken

Reply

23 indir July 15, 2011

Hi,
Thanks chepas, it’s worked!

Reply

24 shivani July 23, 2011

hiiiiiiiiii how to write the code in file……..

Reply

25 mansi July 23, 2011

how to make folder in unix…………

Reply

26 Manish August 4, 2011

@Shivani and @ Mansi

1st ask wat is UNIX?
u guys are kidding, right? :)

Reply

27 C Pimento August 7, 2011

Thank you !

Reply

28 nsu123 September 26, 2011

Hi,
I have a couple symboliclink questions:

1. How can I create a symboliclink between two filesystems?
Do I need to have a mount point to the second filesystem first then create a link to it?

2. If I copy a file with that has a symboliclink to another filesystem, will it lose the link?

Thanks.

Reply

29 Gopikant September 27, 2011

What is the basic use of a soft link or sym link?

Reply

30 chandra sekhar October 1, 2011

soft link maintains duplicate data in two files.
if you link one file with another,change in one file reflected to another file also.

Reply

31 seafangs January 17, 2012

Thanks, this was the info I needed

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">




Previous post:

Next post: