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

What is a soft (“symbolic”) link? How do I create a soft link (symbolic link) under UNIX or Linux operating system?

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. This guide explains how to use the ln command to create symbolic/soft links.

ADVERTISEMENTS

Linux / UNIX create soft link with ln command

Symbolic links can be made to directories as well as to files on different filesystems or different partitions.

Two types of links

There are two types of links

  • symbolic links (also known as “soft links” or “symlinks”): 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 under Unix and Linux?

Soft links are created with the ln command. For example, the following would create a soft link named link1 to a file named file1, both in the current directory
$ ln -s file1 link1
To verify new soft link run:
$ ls -l file1 link1
Sample outputs:

-rw-r--r--  1 veryv  wheel  0 Mar  7 22:01 file1
lrwxr-xr-x  1 veryv  wheel  5 Mar  7 22:01 link1 -> file1

From the above outputs it is clear that a symbolic link named ‘link1’ contains the name of the file named ‘file1’ to which it is linked.

How to use the ln command

So the syntax is as follows to create a symbolic link in Unix or Linux, at the shell prompt:
$ ln -s {source-filename} {symbolic-filename}

For example create a 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

Sample outputs:

lrwxrwxrwx 1 vivek  vivek    16 2007-09-25 22:53 index.php -> /webroot/home/httpd/test.com/index.php

You can now edit the soft link named /home/vivek/index.php and /webroot/home/httpd/test.com/index.php will get updated:
$ vi /home/vivek/index.php
Your actual file /webroot/home/httpd/test.com/index.php remains on disk even if you deleted the soft link /home/vivek/index.php using the rm command:
$ rm /home/vivek/index.php ## <--- link gone ##
## But original/actual file remains as it is ##
$ ls -l /webroot/home/httpd/test.com/index.php

Creating Symlink to a directory

The syntax remains same:
$ ln -s {source-dir-name} {symbolic-dir-name}
For example, create a symbolic link from the /home/lighttpd/http/users/vivek/php/app/ directory to the /app/ directory you would run:
$ ln -s /home/lighttpd/http/users/vivek/php/app/ /app/
Now I can edit files using /app/
$ cd /app/
$ ls -l
$ vi config.php

How to overwrite symlinks/Soft link

Pass the -f to the ln command to overwrite links:
ln -f -s /path/to/my-cool-file.txt link.txt

How to delete or remove symlinks/soft links

Use the rm command to delete a file including symlinks:
rm my-link-name
unlink /app/
rm /home/vivek/index.php

Getting help about the ln command

Type the following ln command:
$ man ln
$ ln --help

ln command option Description
--backup make a backup of each existing destination file
-b like --backup but does not accept an argument
-d allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser)
-f remove existing destination files
-i prompt whether to remove destinations
-L dereference TARGETs that are symbolic links
-n treat LINK_NAME as a normal file if it is a symbolic link to a directory
-P make hard links directly to symbolic links
-r create symbolic links relative to link location
-s make symbolic links instead of hard links
-S override the usual backup suffix
-t specify the DIRECTORY in which to create the links
-T treat LINK_NAME as a normal file always
-v print name of each linked file
--help display this help and exit
--version output version information and exit

Conclusion

You learned how to create a symbolic link in Linux using the ln command by passing the -s option. See ln command man page here for more information.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
63 comments… add one
  • Dominic Jun 13, 2008 @ 11:22

    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?

    • Cameron Apr 14, 2012 @ 15:29

      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.

      • Chris Oct 12, 2012 @ 13:58

        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

        • Alex Sep 30, 2015 @ 5:04

          I run a server on LAN, therefore I don’t give a (“naughty-word”) about it, I do chmod 7777. Just saying, ha ha ha.

          • Kal Jan 21, 2016 @ 15:56

            wow, lol.. I hope you never become a sysadmin

            • H Feb 22, 2017 @ 10:20

              Yes, he just simply throws a shortcut and not a solution

          • Ben Yanke May 5, 2016 @ 2:46

            Because there’s no possible reason to follow good practice when nobody’s watching, right?

            [ / sarcasm ]

  • Anil Kumar Jena Jun 23, 2008 @ 23:03

    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.

  • asif Oct 4, 2008 @ 14:06

    how do you create a symbolic link to directories…

    • santosh Jul 22, 2010 @ 12:11

      u cannot create symbolic link to directories.

      • bbithead Sep 15, 2010 @ 4:12

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

        • Flavian Oct 22, 2014 @ 6:28

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

          • joe schmoe Jun 23, 2015 @ 15:49

            No he didn’t. where in the question did he specify he needed a hard link?

        • Malcolm Sep 27, 2015 @ 21:27

          There are a lot of conflicting comments here.

          1. Generally speaking, a directory *is a file* for the purposes of a symbolic link, i.e., you don’t need to do anything different.

          2. The LINK points to the TARGET.

          3. The TARGET is relative to the LINK.

          4. The syntax is ln -s TARGET LINK

          So suppose you have the following hard directory:
          /var/www/App/Vendor/VendorPublicDir/
          and you want to create a symlink to it in your webroot located at
          /var/www/App/Webroot/

          Then do:

          ln -s /var/www/App/Vendor/VendorPublicDir /var/www/App/Webroot/VendorPublicDirLink

          … this creates a LINK in /var/www/App/Webroot named VendorPublicDirLink that points to the TARGET /var/www/App/Vendor/VendorPublicDir

          – or –

          ln -s ../Vendor/VendorPublicDir /var/www/App/Webroot/

          … this creates a LINK in /var/www/App/Webroot named VendorPublicDir (it defaulted to the TARGET name here because the LINK is an existing directory). The LINK points to the TARGET ../Vendor/VendorPublicDir (which is relative to /var/www/App/Webroot; where you run the command from doesn’t matter).

      • joe schmoe Jun 23, 2015 @ 15:49

        Bunch of mental midgets on this board.

        ln -s DIRECTORY_NAME TARGET_DIRECTORY_NAME

  • Vishal Dec 12, 2008 @ 5:59

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

  • samir Dec 22, 2008 @ 5:05

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

    • darthvader Nov 3, 2016 @ 23:49

      hey samir, i’m not doing your homework
      rekt.

  • anusha Feb 10, 2009 @ 8:15

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

    • tango charlie Apr 27, 2011 @ 0:38

      you can search on google

    • Akash May 7, 2012 @ 3:06

      using man command……………….

  • a11an Jun 15, 2009 @ 11:46

    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.

  • Debashis Mohapatra Jul 24, 2009 @ 10:22

    Hi,

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

  • jesse Jul 24, 2009 @ 18:51

    “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.

  • varunreddy Aug 28, 2009 @ 14:43

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

  • Deandre Oct 13, 2009 @ 18:13

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

  • Deandre Oct 13, 2009 @ 18:13

    How do you do that??

  • Hi SUNIL Oct 22, 2009 @ 7:54

    whats the advantage of soft link over Hard link?

  • Hi SUNIL(IEET BADDI) Oct 22, 2009 @ 7:58

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

  • darshan Oct 26, 2009 @ 2:47

    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

  • chepas Jan 3, 2010 @ 18:27


    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

  • links management Oct 3, 2010 @ 10:59

    You can use .htaccess rules to create it

  • Devadas Apr 4, 2011 @ 10:18

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

  • nikesh Jun 13, 2011 @ 19:56

    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.

  • Ndamu Jul 5, 2011 @ 7:09

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

  • indir Jul 15, 2011 @ 3:27

    Hi,
    Thanks chepas, it’s worked!

  • shivani Jul 23, 2011 @ 9:48

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

  • mansi Jul 23, 2011 @ 9:50

    how to make folder in unix…………

    • Manish Aug 4, 2011 @ 12:58

      @Shivani and @ Mansi

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

  • C Pimento Aug 7, 2011 @ 23:18

    Thank you !

  • nsu123 Sep 26, 2011 @ 22:16

    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.

  • Gopikant Sep 27, 2011 @ 7:22

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

    • chandra sekhar Oct 1, 2011 @ 6:00

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

  • seafangs Jan 17, 2012 @ 21:54

    Thanks, this was the info I needed

  • Bharath Feb 10, 2012 @ 5:54

    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

  • sachin W Mar 7, 2012 @ 10:13

    nice support

  • adhishesh May 17, 2012 @ 12:59

    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.

  • Andrew Jun 22, 2012 @ 22:20

    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!

  • laxman Feb 1, 2013 @ 0:59

    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

  • laxman Feb 1, 2013 @ 0:59

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

  • laxman Feb 1, 2013 @ 1:01

    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

    • Ndukaku Onyekachi Jul 12, 2015 @ 17:30

      symbolic link is use to share file between two people. For example, Mr A hired Mr B and what Mr.B to work in his file, instaed of giving him his system, he linked his file to Mr.B with New file name. Name whatever work Mr.B do will update to Mr.A file. and you can do that with this command,
      ln -s /home/Mr.A/Mr-file.txt /home/Mr.B/newfile.txt

      hope this helps

  • Vignesh Jul 16, 2013 @ 11:17

    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.

  • mike Jan 20, 2014 @ 18:05

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

  • Jeremy Sep 10, 2014 @ 14:28

    ^—- Mike, I think i like you …lol seriously people 1-800-534RCH-G00Gl3

  • aby Dec 17, 2014 @ 5:06

    i creted ldap users. now i want to share some directories to each ldap users with different permissions from server. how will i do that?

  • serg Sep 17, 2015 @ 17:46

    It dind’t worked
    I use Ubuntu 14.10
    I have solved that running sublime from the startup and calling again for each file I edit

  • jason Nov 20, 2015 @ 15:34

    Your current directory is sample_dir. Create a hard link to cars2, called cars3 within sample_dir?

  • Tank Dec 15, 2015 @ 19:48

    This is the hugest pet peeve when people try to explain *Nix stuff. You’re explaining it to people who already understand what ln does. Your use of “target-filename” “symbolic-filename” don’t make sense for someone who doesn’t understand ln to begin with.

    Your description doesn’t make sense either

    “For example create softlink for /webroot/home/httpd/test.com/index.php as /home/vivek/index.php, enter the following command:”

    Should read something like “To symbolicly link an existing file ‘/webroot/home/httpd/test.com/index.php’ to this location ‘/home/vivek/index.php'”

    Nothing is explicitly indicating what is symbolic and what actually exists. Either that or use terminology like source / destination. As both arguments here can be interpreted as targets per how other *nix commands are used and documented.

  • lost_at_sea Apr 26, 2016 @ 16:05

    I am a complete newbie to this so can anyone tell me where exactly the symlink code goes?
    Does it go in .htaccess or some other place?
    Thanks!

  • Baha Jul 27, 2016 @ 20:25

    Hi, everyone! This is so far what did I achieve. Can someone help with symbolic links? I couldn’t create a symbolic link between diretories.
    [root@localhost folder2]# cd /root/folder
    [root@localhost folder]# ll
    total 8
    -rw-r–r–. 1 root root 19 Jul 27 13:07 file1
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file2
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file3
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file4
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file5
    -rw-r–r–. 1 root root 0 Jul 27 12:20 kgt
    -rw-r–r–. 1 root root 11 Jul 27 12:56 kgz
    [root@localhost folder]# ln -s kgz /root/kgz.link
    [root@localhost folder]# ls -l
    total 8
    -rw-r–r–. 1 root root 19 Jul 27 13:07 file1
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file2
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file3
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file4
    -rw-r–r–. 1 root root 0 Jul 27 12:20 file5
    -rw-r–r–. 1 root root 0 Jul 27 12:20 kgt
    -rw-r–r–. 1 root root 11 Jul 27 12:56 kgz
    [root@localhost folder]# cd
    [root@localhost ~]# ls
    1 Desktop Downloads folder2 install.log.syslog Music Pictures Templates
    anaconda-ks.cfg Documents folder install.log kgz.link passwd Public Videos
    [root@localhost ~]# cd folder
    [root@localhost folder]# ls
    file1 file2 file3 file4 file5 kgt kgz
    [root@localhost folder]# cat kgz
    KGZ
    KYRGYZ
    [root@localhost folder]# cd
    [root@localhost ~]# cat kgz.link
    cat: kgz.link: No such file or directory
    [root@localhost ~]#

  • siddu Dec 28, 2016 @ 17:56

    Before executing the command, should i change my directory to the parent directory so that the the file(target file name) which i intended to point to an another one(symbolic file name) points out in every other child directories present inside the parent directory?? OR can i just execute the command irrespective of which directory i am in??
    PLEASE HELP

  • Gavin Mar 6, 2017 @ 22:19

    This is an APPALLING, TERRIBLE page. You don’t even explain the order of the ln arguments. Which is the link, and which is the file?!?!?!?!

    It truly is bad.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.