Difference between Softlink and Hardlink

Here we will see what’s exactly is soft-link first.

  1. Symbolic Link – Links the files and directories located across diferent file systems.
  2. A Softlink has different inode number, Original File and Soflinked file don’t have same inode number.
  3. Softlink can be created across different Filesystems.
  4. Deleting original folder will break Symlink

How to Identify Softlink
You can do ls -ltr, you will see a “l” in the start of the permissions.

[root@server ]# ls -ltr
lrwxrwxrwx 1 root root 24 Dec  9 08:57 symlink -> /usr/local/orginalfolder

How to create a softlink
Create a directory

[root@server ]# mkdir /usr/local/orginalfolder

and now you can create symlink using.

[root@server ]# ln -s /usr/local/orginalfolder symlink

In the output below, you will notice that Soft link and Orginal folder are different.

[root@server ]# stat symlink
  File: ‘symlink’ -> ‘/usr/local/orginalfolder’
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: fd01h/64769d    Inode: 256014      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:57:22.942621767 +0000
Modify: 2019-12-09 08:57:19.394593754 +0000
Change: 2019-12-09 08:57:19.394593754 +0000
 Birth: -

[root@server ]# stat /usr/local/orginalfolder
  File: ‘/usr/local/orginalfolder’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd01h/64769d    Inode: 395887      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 08:56:45.194323755 +0000
Modify: 2019-12-09 08:56:44.055314766 +0000
Change: 2019-12-09 08:56:44.055314766 +0000
 Birth: -

What’s the Hard-link.

  1. The main thing is you can’t create Harlink for Directories.
  2. Unlike Softlink, Hardlink can’t be created across different file-systems.
  3. Hardlinks share same inode number.

How to create a Hardlink

[root@server ]# touch originalFile.txt
[root@server ]# ln originalFile.txt hardLink.txt

Now you see a hard-link is created with same-content.

[root@server ]# cat originalFile.txt hardLink.txt
Hey Buddy!!
Hey Buddy!!

You will also notice that both have same inode number.

[root@server ]# stat originalFile.txt hardLink.txt
  File: ‘originalFile.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -
 
 File: ‘hardLink.txt’
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 256018      Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-09 09:17:11.070108603 +0000
Modify: 2019-12-09 09:07:39.234487185 +0000
Change: 2019-12-09 09:07:58.344638059 +0000
 Birth: -

and if you remove original file it does not affect Hardlink

[root@server ]#  rm -fv originalFile.txt
removed ‘originalFile.txt’

[root@server ]#  ll
total 4
-rw-r--r-- 1 root root 12 Dec  9 09:07 hardLink.txt

[root@server ]#  cat hardLink.txt
Hey Buddy!!
[root@server ]# 

Leave a Reply

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