Understanding UNIX / Linux symbolic (soft) and hard links

by Vivek Gite on January 30, 2006 · 14 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

We're here to help you make the most of sysadmin work. So, subscribe!

{ 14 comments… read them below or add one }

1 Balakumar June 26, 2008

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

Reply

2 kaos December 9, 2008

Hard links cannot “links” directories?

Reply

3 Vivek Gite December 9, 2008

@ kaos,

Yes. Here is output from my desktop:

ln /etc /tmp/test
ln: `xy': hard link not allowed for directory

Reply

4 ROBERTSON April 13, 2009

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

Reply

5 lv June 2, 2009

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

Reply

6 hw June 4, 2009

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

Reply

7 Morph July 14, 2009

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

Reply

8 ramesh February 10, 2011

thank u vivek very good infomation
and nice explain.

Reply

9 Anjali March 14, 2011

Hi

I have created the softlink…….but not working

Reply

10 Aman Bedi July 20, 2011

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

Reply

11 mallick August 29, 2011

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

Reply

12 Srinivas Kotaru October 5, 2011

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?

Srinivas

Reply

13 tordeu January 12, 2012

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

Reply

14 mahesh February 3, 2012

how the soft links and hard links will be helpful?

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 10 + 11 ?
Please leave these two fields as-is:
Are you a human being? Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: