Unix Create a Symbolic Link

How do I create a symbolic links under Linux or Unix like operating systems using command line options?

You need to use the ln command. It is a standard Unix / Linux / BSD command to create links to files. There are two types of links under UNIX, hard and soft link:

Tutorial details
Difficulty level Easy
Root privileges No
Requirements ln command
Est. reading time N/A

Hard link vs. Soft link in Linux or UNIX

[a] Hard links cannot links directories ( cannot link /tmp with /home/you/tmp)

[b] Hard links cannot cross file system boundaries ( cannot link /tmp mounted on/tmp to 2nd hard disk mounted on /harddisk2)

[c] Symbolic links refer to a symbolic path indicating the abstract location of another file

[d] Hard links, refer to the specific location of physical data.

UNIX create a symbolic link command

To create a symbolic link, enter:
$ ln -s {/path/to/file-name} {link-name}
$ ln -s /shared/sales/data/file.txt sales.data.txt
$ vi sales.data.txt
$ ls -l sales.data.txt

How do I delete a symbolic link?

To delete a link, enter:
$ rm {link-name}
$ rm sales.data.txt
$ ls -l
$ ls -l /shared/sales/data/file.txt

If you delete the soft link itself (sales.data.txt) , the data file would still be there ( /shared/sales/data/file.txt ). However, if you delete /shared/sales/data/file.txt, sales.data.txt becomes a broken link and data is lost.

UNIX create a hardlink command

To create hard link, enter (without the -s option):
$ ln {file.txt} {hard-link}
$ ln /tmp/file link-here

How do I delete a hard link?

You can delete hard link with the rm command itself:
$ rm {hard-link}
$ rm link-here

If you delete a hard link, your data would be there. If you delete /tmp/file your data still be accessible via link-here hard link file.

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
5 comments… add one
  • Puneet Verma May 6, 2009 @ 12:17

    I am trying to create a hard link to PHP file after installation of PHP4 as a CGI .
    My Current directory is clients cgi-bin and want to create a link to usr/local/php4/bin/php but getting following error:

    ln: creating hard link `php’ to `/usr/local/php4/bin/php’: Invalid cross-device link

  • Atul Khachane Jun 8, 2009 @ 15:34


    You may delete soft link using below command
    # unlink {link name};

  • Sean M. Burke Oct 18, 2010 @ 15:29

    I could never get the order of the two things in “ln -s X Y” right, and ended up clobbering Ys with dangling links to X. So I wrote this little sanity-checker wrapper around it: lns.
    It does stuff like making sure that your link won’t overwrite anything, and that it’s a link to something that already exists. Just common sense, but sense that I didn’t have.

    lns: http://interglacial.com/~sburke/pub/lns.html

    I’ve been using it for ten years, and I think it’s the most useful program I’ve ever written. (crontab2english is a runner-up, people tell me)

  • Jidnesh Dec 4, 2012 @ 11:47

    If you want to create a symbolic link for multiple files under one directory e.g. want to have all jar files of java to ant lib you can use this feature of linux. This will solve your purpose as well as save the space with reduced redundancy.
    One can use for loop to create a sym link of multiple files of a directory as below,

    # for F in `ls /usr/share/java/ext/*.jar`; do `ln -s $F  /usr/share/ant/lib/`basename $F /usr/share/java``; done  
  • Jaminyah Feb 21, 2014 @ 18:00

    Here is a youtube comment I made based on solving a lua-5.2.3 update issue on CentOS 6.5. The issue here is that your symbolic link tutorial was the key to solving my update issue with lua-5.2.3.

    So here is a dilemma I ran into with CentOS 6.5. # lua -v returns lua-5.1.4 while the latest version is now lua-5.2.3. lua.org/ makes no mention of how to deal with updating the old lua version. Here is what works for me on CentOS 6.5:
    1. sign in as administrator on your linux machine
    2. # cd /opt/
    3. # curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz
    4. # tar zxf lua-5.2.3.tar.gz
    5. # cd lua-5.2.3
    6. # make linux test
    7. # lua -v
    8. Note: If it returns lua-5.1.4 then
    9. # which lua /* /usr/bin/lua is returned */
    10. # rm /usr/bin/lua /* removes old installation */
    11. # ln -s /opt/lua-5.2.3/src/lua /usr/bin/lua /* symbolic link to version 5.2.3 */
    12. lua -v /* should return lua-5.2.3 */
    12. which lua /* should return /usr/bin/lua */
    Since this latest .rpm is still lua-5.1.4 yum install didn’t do an update. Hope this is of help to some.

    Thanks much.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum