Difference between Softlink and Hardlink – Unix/Linux

Good day techies!!! This is essential to know about links (both soft link or Symlink and hard link) because we are Linux techies. It’s vital to knowing about how Linux/Unix environment works!! Here I’m explaining both Soft and Hard links with appropriate examples.

Both soft link and hard link are used to make links between files/directories.

I’m pretty sure about those examples I explain here will be much helpful to you to know this deeply 🙂 This will definitely give a clear idea on this concept.

What is Soft link / Symbolic link / Symlink?

fig15-07

This is very similar to what we know about the Shortcuts option available in Windows. It contains the information about the destination file. Symbolic link points the link to the file or directory name. This is the main difference between both links. As the soft link is pointing to the file or directory name, the link will not be accessible if the original file or folder is deleted/removed.

More details are added under the deference b/w section.

What is hard link?

It’s a bit difference object as compared to Soft link. Hard link is the reference or pointer to the exact file.
That’s why we can access if the original file is removed or moved from the original location.
Hard link is a label stored in a directory structure that refers the OS to the file data when it is accessed.

The important part is the hard link is more close or related to the original file.

Differences between symbolic link or soft link and hard link

Quick view table

Soft linkHard link
ln -sln
Can create soft link for both files and directories.Files only.
Symbolic link points the link to the file or directory name.Hard link is the reference or pointer to the exact file.
The link will not be accessible if the original file or folder is deleted/removed.Can access.
Different inode value.Same inode number.
Soft link is possible in different partition.Not possible.

More details with some examples and proof

Hardlink

1, The ‘ln’ command by default create hard links.

Syntax:

# ln $Sourcefile $Link

Example:

[[email protected] home]# touch crybit
[[email protected] home]# echo "Hardlink test" > crybit 
[[email protected] home]# cat crybit 
Hardlink test

Create a hardlink of this file to a file in your / location:

[[email protected] ~]# ln /home/crybit link
[[email protected] ~]# cat link 
Hardlink test

2, Hard link not allowed for directory:

Proof:

Create a test diretory under /home location:

[[email protected] home]# mkdir hard

Try to link that directory:

[[email protected] ~]# ln /home/hard/ link 
ln: `/home/hard/': hard link not allowed for directory

3, Original file and linked will have the same inode number:

Proof:

You can find out the inode value by using the ‘ls’ command with ‘-i’ switch:

# ls -li $file
[[email protected] ~]# ls -li /home/crybit 
147144 -rw-r--r--. 2 root root 0 Jan 31 04:48 /home/crybit
[[email protected] ~]# ls -li link 
147144 -rw-r--r--. 2 root root 0 Jan 31 04:48 link
In the above example the inode value is "147144".

4, We can access, if the original file is removed/moved from server.

Proof:

Remove the original file:

[[email protected] home]# cat crybit 
Hardlink test
[[email protected] home]# rm -rf crybit
Access the link file:

[[email protected] home]# cd
[[email protected] ~]# cat link 
Hardlink test

5, Pointing link to the data location, that’s why we can access if the original file is removed from the location.

6, Hard link is not possible in different partition.

Proof:

1, Create a test file in one partition:

[[email protected] boot]# touch test

2, try to link from another location(Partition):

[[email protected] boot]# cd
[[email protected] ~]# ln /boot/test link
ln: creating hard link `link' => `/boot/test': Invalid cross-device link

Softlink

1, The ‘ln’ command with switch ‘s’ is used to create the softlink

Syntax:

# ln -s $Sourcefile $Link
[[email protected] ~]# cd /home/
[[email protected] home]# touch crybit
[[email protected] home]# echo "Softlink test" > crybit
Create softlink from another location:

[[email protected] home]# cd
[[email protected] ~]# ln -s /home/crybit link
[[email protected] ~]# ll link
lrwxrwxrwx. 1 root root 12 Jan 31 13:10 link -> /home/crybit
[[email protected] ~]# cat link
Softlink test

2, We can softlink directory also

Proof:

Create a test directory in /home location:

[[email protected] home]# mkdir test

Create a soft link to /home/test directory from another location:

[[email protected] ~]# ln -s /home/test/ linkdir
------
lrwxrwxrwx. 1 root root    11 Jan 31 13:15 linkdir -> /home/test/
------

3, Original file/directory and the linked file/directory are having different inode value.

Proof:

[[email protected] home]# ll -i crybit
147144 -rw-r--r--. 1 root root 14 Jan 31 13:10 crybit
[[email protected] home]# cd
-----
[[email protected] ~]# ll -i link
525994 lrwxrwxrwx. 1 root root 12 Jan 31 13:10 link -> /home/crybit

4, We can’t access, if the original file/diris removed/moved from server.

Proof:

[[email protected] home]# rm -rf crybit 
[[email protected] home]# cd
[[email protected] ~]# cat link
cat: link: No such file or directory

5, Pointing link to the file/dir name, that’s why we can’t access if the original file/dir is removed from the location.

6, Soft link is possible in different partition.

Proof:

[[email protected] ~]# cd /boot/
[[email protected] boot]# mkdir test
[[email protected] boot]# cd
[[email protected] ~]# ln -s /boot/test/ linkdir
[[email protected] ~]# ll -d linkdir
lrwxrwxrwx. 1 root root 11 Jan 31 13:31 linkdir -> /boot/test/

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

More:

groupdel, groupmems, groupmod, useradd , usermod , chgrp, chown, ls, head, tail, top, ps, find, crontab

Arunlal Ashok

Linux Server Administrator. I'm managing Linux servers since 2012. I started this blog to share and discuss my ideas. Check My Profile!! in uPwork (oDesk) and let me know if you need any assistance. Thanks!!

You may also like...

4 Responses

  1. Ankitha.u says:

    Tq it is good to understand

  2. md sameer says:

    Really it’s nice article Ashok.

Leave a Reply

Your email address will not be published. Required fields are marked *