What is a hard and symbolic (soft) link in Linux or Unix?

Inodes are associated with precisely one directory entry at a time. However, with hard links, it is possible to associate multiple directory entries with a single inode. Each file on Linux or Unix comes with an inode containing metadata about the file. To create a hard link use the ln command as follows:
ls /path/to/source /path/to/link
ln old_dir new_link_dir
ls [options] /path/to/source /path/to/link
## create a hard link ##
ln /home/vivek/my-awesome-long-file.txt /home/vivek/file
## create a symbolic link ##
ln -s /etc/resolv.conf /tmp/test
ls -l /tmp/test /home/vivek/file

UNIX and Linux symbolic (soft) and hard links
Above commands create a link to my-awesome-long-file.txt and /etc/resolv.conf as /home/vivek/file and /tmp/test respectively.

What is a Symbolic or soft link in Linux / Unix?

Symbolic links refer to:

A symbolic path indicating the abstract location of another file. It is like a shortcut in Microsoft Windows operating system.

What is a hard link in Linux or Unix?

Hard links refer to:

The specific location of physical data. It an essentially a label or name assigned to a file.

Hard link vs. Soft link in Linux or UNIX

  • Hard links cannot link directories.
  • Cannot cross file system boundaries.

Soft or symbolic links are just like hard links. It allows to associate multiple filenames with a single file. However, symbolic links allows:

  • To create links between directories.
  • Can cross file system boundaries.

These links behave differently when the source of the link is moved or removed.

  • Symbolic links are not updated.
  • Hard links always refer to the source, even if moved or removed.

How do I create symbolic link in Linux?

You can create symbolic link with ln command:
ln -s source_file my_link
ln -s /path/to/file1.txt /path/to/file2.txt
ls -s /etc/hosts /tmp/file
ls -ali /tmp/file /etc/hosts

Above command will create a symbolic link to /etc/hosts as /tmp/file.

Symbolic link creation and deletion

Let us create a directory called foo using mkdir command $ mkdir foo
$ cd foo

Copy /etc/resolv.conf file using the cp command, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample outputs:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:19 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

Now create soft link to resolv.conf, enter:
$ ln -s resolv.conf alink.conf
$ ls -ali

Sample outputs:

total 152
1048600 drwxr-xr-x   2 vivek vivek   4096 2008-12-09 20:24 .
1015809 drwxrwxrwt 220 root  root  143360 2008-12-09 20:19 ..
1048602 lrwxrwxrwx   1 vivek vivek     11 2008-12-09 20:24 alink.conf -> resolv.conf
1048601 -rwxr-xr-x   1 vivek vivek    129 2008-12-09 20:19 resolv.conf

The reference count of the directory has not changed (total 152). Our symbolic (soft) link is stored in a different inode than the text file (1048602). The information stored in resolv.conf is accessible through the alink.conf file. If we delete the text file resolv.conf, alink.conf becomes a broken link and our data is lost:
$ rm resolv.conf
$ ls -ali

If alink.conf was a hard link, our data would still be accessible through alink.conf. Also, if you delete the soft link itself, the data would still be there. Read man page of ln for more information.


To create a symbolic link in Linux and Unix, at the shell prompt, enter:
ln -s source new_soft_link
ls -ali source new_soft_link

And to create a hard link in Linux and Unix, at the shell prompt, enter:
ln source new_hard_link
ls -ali source new_hard_link

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

🐧 35 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
35 comments… add one
  • Balakumar Jun 26, 2008 @ 7:01

    Thank u Vivek Gite Sir,
    for posting very nice article about linux filesystem…

  • kaos Dec 9, 2008 @ 14:34

    Hard links cannot “links” directories?

  • 🐧 nixCraft Dec 9, 2008 @ 14:37

    @ kaos,

    Yes. Here is output from my desktop:

    ln /etc /tmp/test
    ln: `xy': hard link not allowed for directory
    • Serrano May 6, 2012 @ 18:28

      Vivek, what kaos meant was: the second word “links” should be written without the “s” in that sentence. It should be: “Hard links cannot link directories”.

  • ROBERTSON Apr 13, 2009 @ 6:35

    Hardlink or softlink is only for files and not for folders.
    /etc is a folder name. Choose any file name under it
    $ln /etc/test.log /tmp/test_file

  • lv Jun 2, 2009 @ 3:48

    That is not really correct. Hard links cannot link directories however soft links can

  • hw Jun 4, 2009 @ 21:13

    soft links are often used to link to directories, e.g.
    cdrom -> /media/cdrom/
    floppy -> /media/floppy/

  • Morph Jul 14, 2009 @ 5:40

    Ya the links cannot be used for directories and also you should not change the default file permissions of symbolic links file by using chmod command lest they will grant the user all the rights to the file. The sys admins therefore disable access to the /etc directory because if you had access to the directory a simple symbolic link to /etc/passwd could get you all the access you want to the system.
    see this link

  • ramesh Feb 10, 2011 @ 8:28

    thank u vivek very good infomation
    and nice explain.

  • Anjali Mar 14, 2011 @ 13:06


    I have created the softlink…….but not working

  • Aman Bedi Jul 20, 2011 @ 16:42

    @ Anjali

    please let us know the output and how are you trying to access the softlink?

    if you open nautilus and try to type the path of the soft link, it will appear as a short-cut (like in windows).

    Try to browse it in X windows.

  • mallick Aug 29, 2011 @ 10:07

    when i right click on a file then how the os gets it’s properties file. i.e in os where those files are stored

  • Srinivas Kotaru Oct 5, 2011 @ 18:49

    I knew that hard links cannot be linked to folders,However unable to find reason behind this? I did lot of googling but still unable to get correct answer Can somebody give me reason for this in simple english?


  • tordeu Jan 12, 2012 @ 15:27

    @Srinivas Kotaru:

    Hard links to directories and not supported, because you would be able to create “loops”.
    Imagine you would go into a folder “a” and create a hard link “b” that would link to the folder “a”. This would create and endless loop. Imagine you would tar the directory a now.
    Tar would go into directory a and find a directory b in it. Then it will try to go into b, but because it is a hard link to a, it will be in folder a again. There, it would again see the folder “b” and when it does into this folder, because of the hard link, end up in a again. And this would never stop, because you created a circle.
    In this example it’s easy to figure out. But the situation might be more complex. A folder a might include – somewhere deep inside – a link to a different folder b and inside b there might be a link to a folder c and somewhere within c there would be a link to folder a. Now you have a circle again.
    It’s not that it’s not possible to prevent programs from hanging etc., but they hard links to directories are a problem and it was decided to not allow them to make things easier.
    Though, Apple uses them in Mac OS X for Time Machine, but does not allow the user to create them.

  • mahesh Feb 3, 2012 @ 5:10

    how the soft links and hard links will be helpful?

  • Chaitanya Feb 24, 2012 @ 10:17

    i have one file and one link file with the same filename as resolv.conf
    lrwxrwxrwx 1 vivek vivek 11 2008-12-09 20:24 alink.conf -> resolv.conf
    -rwxr-xr-x 1 vivek vivek 129 2008-12-09 20:19 resolv.conf
    How can i open the content of the file resolv.conf which has a link file i.e. 1st one.

    • cordel Mar 20, 2012 @ 0:08

      If you try to open either file with an editor or any thing for that matter they both will open resolv.conf.
      resolv.conf is your real file and alink.conf is a symbolic link pointing to resolv.conf.
      Though I’m sure you probably figured this out by now.

  • tordeu Mar 21, 2012 @ 9:27


    Softlinks and hardlink can be used for various things (organizing files is one of them).

    Let me explain what you might do with links with the following example:

    Let’s assume you have pictures in “~/Pictures” and music in “~/Music”. In “~/Music”, there is a folder for every artist you like and within that folder there is a folder for every album, which contains the songs of the artist; so you might have a folder “~/Music/Josh Woodward” and in that folder are the albums of Josh Woodward (that gives you “~/Music/Josh Woodward/Ashes” etc.).

    You also have some Pictures of Josh Woodward in ~/Pictures/People/Josh Woodward.

    You like to have all your pictures in ~/Pictures and all your music in ~/Music, but you don’t like that those folders are not close together. The pictures and the music of one artist are in completely different folders and you would also like to have all the music and all the pictures of an artist in one folder.

    So you might want to create the folder “~/Artists” and in that folder you create a folder for every artist and within that you have a folder “Music” and a folder “Pictures” that contains the music and pictures of that artist. In this example you would have the folders ~/Artist/Josh Woodward/Music and ~/Artist/Josh Woodward/Pictures.
    Now you have want you wanted and all the files of one artist are packed together in one folder.

    The first idea would be to copy all the files into the new folders, but that leads to three main problems:
    1. every file is saved twice on the disk, so you need twice the disk space, because every song will be in ~/Music/ and in ~/Artists//Music and so on.
    2. if you would change the tags of a Picture in ~/Pictures/… the same Picture in ~/Artists would still have the old tags, you if you change a Picture, you need to copy it to the other locations to update the version there.
    3. every new file needs to be saved to two places, which adds more work.

    Problem two and three would be solved by creating scripts, but synchronizing the content could take some time. And problem one would still remain.

    And now think you might want to go even further and for every artist you want to organize your albums in several ways: in “~/Music/by-artist” you have all your music organized by artists; in “~/Music/by-year” you have all albums organized by year and in ~/Music/by-title” you just have all the albums without any further organization. To maintain such a structure can only be done with scripts and every time you add a new song somewhere, you need to run a synchronization script.

    THIS is where links come into play.

    In the “~/Artist/” folder, you do not create a folder “Music” and a folder “Pictures” and copy all the files; you just create a symbolic link “Music” to the folder “~/Music/” and a link “Pictures” to the folder “~/Pictures/People/”.

    Now, you can access the music and pictures through “~/Artists/” folder. But the files only need to be stored once on the hard disk AND if you put a new album in “~/Music//” it is immediately accessible in “~/Artists//Music”. And if you change the tags of a Picture, it does not matter if you go to “~/Artists/…” or to “~/Pictures/…”, the changes will be accessible in both places.

    You can’t create hard links in this case, because we are dealing with directories. But you can use hard links for the same thing. Hard links are just an additional name for the same file. So you in this example, you could create the folder “~/Artists///” and then in that folder create hard links for all the audio files.
    In this case, adding new albums or new files to an album will not be visible in the other place until you create hard links for all new files as well. Hard links do have some advantages, however, because sometimes soft links will create problems in applications whereas hard links are “real files”. And the most important thing: If you create 100 symlinks to a single file and you delete that single file, all symlinks will stop working, because the symlinks are just links (like on the internet, remember?) and if you remove a site from the internet, all the links to it will stop working.

    With hard links that will not happen; but hard links only work in the same file system.

    I hope this helped a bit.

  • connect@me Jun 1, 2012 @ 10:00

    Hi tordeu,

    Nice explanation i need little more explanation about hard link with examples..

    Thanks in advance.

  • tordeu Jun 12, 2012 @ 1:16

    sorry for taking so long.

    Hard links are essentially just additional names for the same file. A file in linux can have more than one name and be in more than one directory at the same time.
    This might be a weird concept at first, because most people do not really use it, but it has some advantages (and disadvantages, depending on what you want to achieve).

    Let’s do this as an exercise. Create a file named “test.txt” in your homefolder, open it with a text editor and add some text to it.
    Now open a terminal and go to your home directory (should not be necessary, because a new terminal should start at your homedirectory, but still:


    Now we can create a hard link and a soft link to this file and name them test_hard.txt and text_soft.txt, respectively:

    ln test.txt test_hard.txt
    ln -s text.txt test_soft.txt

    If you look at your home folder now, you will see 3 files: test.txt, test_hard.txt and test_soft.txt.
    If you open any one of them with a text editor, you should always get the same content.

    Now rename test.txt to test_new.txt.

    If you try to open test_hard.txt and test_soft.txt now, you will notice that test_hard.txt works just fine, but test_soft.txt does not. The reason why the soft link does not work anymore is because a softlink is basically a file that only contains the name of another file. When you created the soft link, you created a soft link to a file named “test.txt”.
    If you now open the test_soft.txt, linux will know that it is soft link and then try to load the file the soft link is pointing to, which is test.txt. But test.txt is not there anymore, because you renamed it, so the file can not be loaded.

    test_hard.txt works differently. A hard link does not just remember the path and name of the file where it links to, a hard link points directly to the data on the disk (well, the inode, to be exact, but that’s not important here), so the two files test_new.txt and test_hard.txt are pointing to the exact same data on the disk.
    test_new.txt and test_hard.txt are now merely two different names for the exact SAME file.
    You can rename each file without affecting the other (because they are both pointing to the data) and can also delete either one and the other one will still work.
    That’s because Linux will remember how many names a file has. When you created the hard link, Linux noticed that there are now two names pointing to the file on the disk. When you delete either one, Linux knows that the other one is still being used and will therefore not delete the data on the disk, but only the filename.

    And it does not matter which one you delete. You can delete test_new.txt and test_hard.txt will work just fine. Linux will only delete the data on the disk once there is no filename pointing to it anymore.

    You can test that it is the same file, by editing it. No matter which one you edit, you will notice that the other does reflect the changes, because it’s just ONE file and test_new.txt and test_hard.txt are just two names for the same file. You can also move one of them to a different folder and it will still work.

    My advice would be to play around with soft and hard links a little bit. Create a simple text file, created soft and hard links to it in different folders and see what happens.
    Just one note: You can only create a hard link to a file on the same filesystem/partition.

    I hope this could help you a little. If not, please feel free to ask more questions. I will try to answer them more quickly.

  • connect@me Jun 12, 2012 @ 5:48

    Hi tordeu,

    thanks a lot tordeu, putting efforts to explain the concepts.. now i m understand soft & hard links working manner.. currently i m working with linux BMR. so i need to analyze the different file system, file types behavior etc.. can u guide me what are things we need to concentrate linux while we take backup and recovered..

    thanks in advance

  • thiyagi Aug 31, 2012 @ 23:14

    Thanks alot guys, the post and all the conversation was informative..;)

  • dimitrios Sep 14, 2012 @ 18:59

    Is it possible to create a link to another server?
    For example ln -s /www/htdocs/link root@ ?

    • Serrano Sep 16, 2012 @ 13:42

      That command should be:
      ln -s root@ /www/htdocs/link

      For as far as I know, that won’t work.

      But you can mount the remote directory instead (e.g. with sshfs). For example:
      sshfs root@ /www/htdocs/dir/

      The remote server needs to have SSH setup for that to work.

    • tordeu Sep 18, 2012 @ 11:14

      Another possibility (besides the already mentioned sshfs) would be nfs, which I would say is the better option if you want to be have access to the files on the server constantly.
      Also, after mounting the server on your computer (no matter which method you use), you can then create symbolic links to those mounted files.

      If your server has a folder foo and inside this folder is a file bar.jpg then you might mount the foo folder to /media/server. After it is mounted, the file bar.jpg will be available under /media/server/bar.jpg.
      Now its possible to create a link to that file with
      ln -s /media/server/bar.jpg bar

      The reason you can’t directly create a soft link is because softlinks just store the path of the file you want to point to, so the file needs to be accessible locally somehow. It’s not possible to use IP addresses to access different systems.
      You need to use something else (like sshfs, nfs, ftpfs, smb etc.) to first mount the files of the server locally. Then the files of the server have a local path through which they can be accessed (usually through /media/… or /mnt/…) and now that they have a local path, you can create a soft link to them.

      • SmilingInSeattle Aug 11, 2013 @ 0:13

        This is very close to what I want to do. I have a NAS server running zfs. I access the zfs filesystem at it’s root with an NFS mount on my linux workstation as well with CIFS mounts on other users Windows boxes.

        When I built the server I knew it would approach 80% capacity within 2-3 years. My original plan was to swap existing 2TB drives for 4TB ones, but I read in forums that resilvering even a 500MB disk could take 1-2 days, leaving one with a degraded file system during that time.

        So, now I’m looking at putting another disk farm on the existing server via an eSATA connection and a port multiplier, then creating another zpool. I’d really like to keep the same directory structure (namewise) on the new file system as on the existing filesystem and make the fact that the users are accessing different zfs file systems transparent.

        Would it be as simple as creating a soft link on the server (BSD) between the original file system and the new one?

        Ex: on BSD, with the original file system at /mnt/nas/data, put the new file system at /mnt/nas/data1 then at root or at /mnt on tthenhe BSD server create a symbolic link: ln -s /mnt/nas/data1 /mnt/nas/data and point the NFS and CIFS mount points at /mnt/nas/data? Below the /mnt/nas/data would be directory one, two, three, four… and some of these would have further subdirectories before getting to files to be accessed.

  • Jouni "rautamiekka" Järvinen Feb 2, 2013 @ 19:24

    So if I change the contents of a file I have a hard link for, what happens next ? The link works or not ?

    • Jouni "rautamiekka" Järvinen Feb 2, 2013 @ 19:26

      I mean, does it work just like it did, with the changes visible just normally.

  • Jouni "rautamiekka" Järvinen Feb 2, 2013 @ 19:30

    What is the performance difference between hard and soft ?

  • ramesh Apr 9, 2013 @ 13:44

    While creating soft link to hard drive, is it necessary to use -F option, like

    ln -sF

  • Dipti May 4, 2013 @ 15:15


    System is implemented with CI framework.
    But symbolik link is not working satisfactory.

    I want to point from abc.com to dps.com’s order controller.
    Actual code is on dps.com domain contoller order.
    Then I want to point that controller as follows from abc.com


    But when I open this url in browser then I am getting error page not found [404 error]
    And When I open it as follows it’s working :


    I do not understand whts going on.
    Please help me.
    In advance Thanks

  • Dipti May 6, 2013 @ 11:03

    I want to create symbolic link .

    I want to point dps.com/order
    order is controller file from abc.com

    so that I can access abc.com/order/[param]

    then I tried to create sym link for this but not working.
    please give me solution.

  • not dave Mar 3, 2014 @ 8:59

    your extensive use of quoting does not clarify

    this article is the opposite of useful: jumbled

    rewrite required

  • Priyabrata Jan 16, 2015 @ 18:40

    1st I created both hardlink (hlink) and softlink (slink) to a single file (a.dat).
    As I know, slink has its own inode, and this inode will point to the inode of a.dat. But hlink does not have its own inode. when we create a hardlink it will directly point to the inode of the a.dat.

    So my qs here is when we are removing Original File (a.dat), because of the existence of a hardlink (hlink), the inode of a.dat still be there in memory.But why we can not access this content by the slink, though slink’s inode points to the inode which is still exist in the memory, whereas hlink can access though it is pointing to the same inode.

    Please help me understand this and correct me if i am wrong..


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