Linux Administrator

What is Difference between Soft Link and Hard Link in Linux/UNIX

If you are a Linux users it is essential to know about links. Soft link also called symlinks . A symbolic link is a file that links to another file or directory using its path. Both soft link and hard link are used to make links between files or directories.

In this article I am going explain both soft link and hard link with appropriate examples.

Difference between Soft Link and Hard Link

I am sure about these examples will be much helpful for you to know this topic in detail and this article definitely provide a clear idea on Linux links.

Soft Link

Soft link is same as what we know about shortcuts option available in Windows. It is an actual link to original file. These links will have a different Inode value. If you delete link points to the original file then the soft link fails and if you delete soft link nothing will happen to file. The reason behind this is the actual file or directory’s inode is different from the soft link.

Hard Link

Hard link is a mirror copy of the original file. These links share the same inodes. Hard link is the reference or pointer to the exact file. If you made some changes to the original or hard linked file then changes will reflect in the other. In hard link we can access if the original file is removed or moved from the original location.

What are Hard Link

1. Hard link always have same inode number
2. Hard link have actual file contents
3. In hard link we can access if the original file is removed
4. You can not create a hard link for a directory
5. ln command is used to create a hard link

What are Soft Link

1. Soft link always have different inode number
2. Soft link contains the path of original file and not to the contents
3. In soft link we can’t access if original file is reoved
4. You can create a soft link to a directory
5. ln -s command is used to create a soft link

Lets see some experimental differences.

Hard Links

First of all crate a new directory called “mytest” and inside crate a new file called “test.txt“.

# mkdir mytest
# cd mytest
# touch test.txt
# echo "Hardlink test" > test.txt

Lets create a hard link to test.txt named “test1.txt“.

# ln test.txt test1.txt

Now show inodes for both files using -i option with ls command.

# ls -il test.txt test1.txt

You will get some output like below.

1482256 -rw-r--r-- 2 root root 21 Nov 5 15:55 test.txt
1482256 -rw-r--r-- 2 root root 21 Nov 5 15:55 test1.txt

As you can see test.txt and test1.txt file have the same inodes number and both files have same permissions and size.

Lets remove the original test.txt file.

# rm test.txt

After deleting original file test.txt just have a look at the content of the “linktest1.txt file.

# cat test1.txt
Hardlink test

You will still get the content of the file.

Now lets try to create hard link of a directory. First of all create a directory called “test“.

# mkdir test

Try to link that directory.

# ln test test1
ln: `test': hard link not allowed for directory

You will get the above output you try to create a hard link of a directory.

Soft Links

Now lets create a soft link for the file test1.txt using below command.

# ln -s test1.txt test2.txt

Lets check the inodes for both file using -i option with ls command.

# ls -il test1.txt test2.txt

You will get some output like below.

1482256 -rw-r--r-- 1 root root 21 Nov 5 15:55 test1.txt
1482226 lrwxrwxrwx 1 root root 5 Nov 5 16:22  test2.txt-> test1.txt

Above you can notice that the inodes are different and the symbolic link has an “l” before the rwxrwxrwx. The permissions are different for the link and the original file because it is just a symbolic link.

Now see the contents.

# cat test1.txt
# cat test2.txt

Now remove the original file test1.txt.

# rm test1.txt

Now check the mytest directory.

# ls 

It will still display symbolic link test2.txt but if you try to list the contents, it will tell you that there is no such file or directory.

# cat test2.txt
cat: test2.txt: No such file or directory

Lets create soft link of a directory.

# ln -s /home/mytest linkdir -> /home/mytest

lrwxrwxrwx. 1 root root    11 Jan 31 13:15 linkdir -> /home/mytest

These are the common difference between hard link and soft link.

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

1 Comment

  • Wonderful work! That is the kind of information that should be
    shared across the web. Shame on Google for now not positioning this post upper!

Leave a Comment