Understanding UNIX / Linux symbolic (soft) and hard links

Posted on in Categories File system, Linux, UNIX last updated January 30, 2006

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. To create a hard link use ln command as follows:
# ln /root/file1 /root/file2
# ls -l

Above commands create a link to file1. Symbolic links refer to:

A symbolic path indicating the abstract location of another file.

Hard links refer to:

The specific location of physical data.

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?

You can create symbolic link with ln command:
$ ln -s /path/to/file1.txt /path/to/file2.txt
$ ls -ali

Above command will create a symbolic link to file1.txt.

Task: Symbolic link creation and deletion

Let us create a directory called foo, enter:
$ mkdir foo
$ cd foo

Copy /etc/resolv.conf file, enter:
$ cp /etc/resolv.conf .
View inode number, enter:
$ ls -ali
Sample output:

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 output:

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.
Continue reading rest of the Understanding Linux file system series (this is part VI):

  • Part I – Understanding Linux superblock
  • Part II – Understanding Linux superblock
  • Part III – An example of Surviving a Linux Filesystem Failures
  • Part IV – Understanding filesystem Inodes
  • Part V – Understanding filesystem directories
  • Part VI – Understanding UNIX/Linux symbolic (soft) and hard links
  • Part VII – Why isn’t it possible to create hard links across file system boundaries?

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

35 comment

    1. 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”.

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

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

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

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

  5. @ 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.

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

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


  8. @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.

  9. hi,
    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.

    1. @Chaitanya
      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.

  10. @mahesh:

    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.

  11. Hi,
    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.

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

    1. That command should be:
      ln -s [email protected]:/www/htdocs /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 [email protected]:/www/htdocs/ /www/htdocs/dir/

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

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

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

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

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

  14. Hello

    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

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

  16. your extensive use of quoting does not clarify

    this article is the opposite of useful: jumbled

    rewrite required

  17. Hi,
    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..


Comments are closed.