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

by on September 25, 2007 · 45 comments· LAST UPDATED September 25, 2007

in , ,

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
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 45 comments… read them below or add one }

1 Dominic June 13, 2008 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?

Reply

2 Cameron April 14, 2012 at 3:29 pm

You need to run
chmod -R 777 /var/www
to set the server root to the right permissions so that pages are viewable.

You may need administrator privileges.

Reply

3 Chris October 12, 2012 at 1:58 pm

Please don’t ever do: chmod -R 777 /var/www or chmod 777 anywhere in your web accessible directories. I won’t bother telling what security risks this will open up. You can google it.

You should be giving the least permission needed to your directories and files on a web server. If you’re not sure what permissions are needed than learn more about how *nix permissions work and how your server works. here is a good place to start: http://en.wikipedia.org/wiki/File_system_permissions#Traditional_Unix_permissions

Reply

4 Anil Kumar Jena June 23, 2008 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.

Reply

5 asif October 4, 2008 at 2:06 pm

how do you create a symbolic link to directories…

Reply

6 santosh July 22, 2010 at 12:11 pm

u cannot create symbolic link to directories.

Reply

7 bbithead September 15, 2010 at 4:12 am

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

Reply

8 Flavian October 22, 2014 at 6:28 am

U missed -d
ln -sd /existing/directory /sym/link/to/that/dir

Reply

9 Vishal December 12, 2008 at 5:59 am

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

Reply

10 samir December 22, 2008 at 5:05 am

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

Reply

11 anusha February 10, 2009 at 8:15 am

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

Reply

12 tango charlie April 27, 2011 at 12:38 am

you can search on google

Reply

13 Akash May 7, 2012 at 3:06 am

using man command……………….

Reply

14 a11an June 15, 2009 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.

Reply

15 Debashis Mohapatra July 24, 2009 at 10:22 am

Hi,

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

Reply

16 jesse July 24, 2009 at 6:51 pm

“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

17 varunreddy August 28, 2009 at 2:43 pm

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

Reply

18 Deandre October 13, 2009 at 6:13 pm

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

Reply

19 Deandre October 13, 2009 at 6:13 pm

How do you do that??

Reply

20 Hi SUNIL October 22, 2009 at 7:54 am

whats the advantage of soft link over Hard link?

Reply

21 Hi SUNIL(IEET BADDI) October 22, 2009 at 7:58 am

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

Reply

22 darshan October 26, 2009 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.

Thanks

Reply

23 chepas January 3, 2010 at 6:27 pm


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

24 links management October 3, 2010 at 10:59 am

You can use .htaccess rules to create it

Reply

25 Devadas April 4, 2011 at 10:18 am

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

Reply

26 nikesh June 13, 2011 at 7:56 pm

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

27 Ndamu July 5, 2011 at 7:09 am

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

Reply

28 indir July 15, 2011 at 3:27 am

Hi,
Thanks chepas, it’s worked!

Reply

29 shivani July 23, 2011 at 9:48 am

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

Reply

30 mansi July 23, 2011 at 9:50 am

how to make folder in unix…………

Reply

31 Manish August 4, 2011 at 12:58 pm

@Shivani and @ Mansi

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

Reply

32 C Pimento August 7, 2011 at 11:18 pm

Thank you !

Reply

33 nsu123 September 26, 2011 at 10:16 pm

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

34 Gopikant September 27, 2011 at 7:22 am

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

Reply

35 chandra sekhar October 1, 2011 at 6:00 am

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

36 seafangs January 17, 2012 at 9:54 pm

Thanks, this was the info I needed

Reply

37 Bharath February 10, 2012 at 5:54 am

Really thanks guyz…. I created link successfully.

CASE: Actually user PC “/” is completely filled, so that user is unable to login. But he is able to login as super user. “/” is 100%.

SOLUTION: I find that /usr and /var is occupying more space, so wat i did is I moved content of /usr directory to /work directory (free space) and did ln -s /usr/include /work/name of the link . Thats it all files are physically in /work direcory but logically it is pointing to /usr/include place.

After rebooting user is able to work with his login, gained space in “/”.

Thanks,
Bharath

Reply

38 sachin W March 7, 2012 at 10:13 am

nice support

Reply

39 adhishesh May 17, 2012 at 12:59 pm

One more thing we need to consider as a security treat, some softwares have default UserID and Password like phpmyadmin and other softwares, after installation of this kind of software’s we need to take care of userID and Password.

Reply

40 Andrew June 22, 2012 at 10:20 pm

I was having some trouble creating a symlink to a directory, and I discovered that it was caused by my using a relative path for the target (e.g. “..”). When I used an absolute path, it worked like a charm!

Reply

41 laxman February 1, 2013 at 12:59 am

Hi All,
i am getting the below output in file
get_history(): getting history
2068, 2986, 6408, 7546, 7555, 7893, 7963, 8219, 8270, 8313, 8410, 8479, 8581, 8607, 8724, 8783, 8804, 8992, 9018, 9018, 9100, 9127, 9157, 9232, 9236, 9269, 9291, 9388, 9565, 9661, 9668, 9850, 9862, 10004, 10004, 10031, 10063, 10834, 12087, 13444
… got 40 historical records
lower: 11354 ? 3000 < 9018 = 0
upper: 11354 ? 9018 < 7000 = -215

9018 is todays number….i want to compaire this number with all above list of number one by one…..please solution mail me on laxmanbyadav@gmail.com

Thanks,
Laxy

Reply

42 laxman February 1, 2013 at 12:59 am

sorry forgot to mension i want this in unix shell or perl…

Reply

43 laxman February 1, 2013 at 1:01 am

Hi All,
I am new with unix, could you please provide some information with example for symbolic link or hardlink …..why and how its use….

Laxy

Reply

44 Vignesh July 16, 2013 at 11:17 am

Hi All,

Currently my application is running on the local HDD partition. The path of application is /usr/local/app.Kindly suggest how we can change the default path to shared storage volume as we are plan to do OS linux clustering.

Reply

45 mike January 20, 2014 at 6:05 pm

why dont u dinks try searching the internet for your dumb questions

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: