Softlink and Hardlink in Linux a detailed explanation

Good day techies!!!

Softlink and Hardlink. This is essential to know about links (both softlink or Symlink and hardlink) 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 the examples I explain here will be much helpful for you to know this concept in detail 🙂 This will definitely give a clear idea on Linux links.

What is Soft link(symbolic link/symlink)?

Softlink and Hardlink
Softlink and Hardlink

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

Softlink and Hardlink. 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.

Something more about Soft and Hard links

Hardlink

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

Syntax:

# ln $Sourcefile $Link

Example:

[root@localhost home]# touch crybit
[root@localhost home]# echo "Hardlink test" > crybit 
[root@localhost home]# cat crybit 
Hardlink test

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

[root@localhost ~]# ln /home/crybit link
[root@localhost ~]# cat link 
Hardlink test

2, Hard link not allowed for directory:

Proof:

Create a test diretory under /home location:

[root@localhost home]# mkdir hard

Try to link that directory:

[root@localhost ~]# 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
[root@localhost ~]# ls -li /home/crybit 
147144 -rw-r--r--. 2 root root 0 Jan 31 04:48 /home/crybit
[root@localhost ~]# 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:

[root@localhost home]# cat crybit 
Hardlink test
[root@localhost home]# rm -rf crybit
Access the link file:

[root@localhost home]# cd
[root@localhost ~]# 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:

[root@localhost boot]# touch test

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

[root@localhost boot]# cd
[root@localhost ~]# 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
[root@localhost ~]# cd /home/
[root@localhost home]# touch crybit
[root@localhost home]# echo "Softlink test" > crybit
Create softlink from another location:

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

2, We can softlink directory also

Proof:

Create a test directory in /home location:

[root@localhost home]# mkdir test

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

[root@localhost ~]# 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:

[root@localhost home]# ll -i crybit
147144 -rw-r--r--. 1 root root 14 Jan 31 13:10 crybit
[root@localhost home]# cd
-----
[root@localhost ~]# 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:

[root@localhost home]# rm -rf crybit 
[root@localhost home]# cd
[root@localhost ~]# cat link
cat: link: No such file or directory

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

6, Soft link is possible in different partition.

Proof:

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

These are the common difference between Softlink and Hardlink..! Happy linking 🙂

What is a Linux container?

Basic: Containerisation helps to isolate processes. You can run your App/Services as an isolated process, running from a distinct image that provides all files necessary to support the processes.

Basically Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

More:

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

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

6 thoughts on “Softlink and Hardlink in Linux a detailed explanation

  1. HI Ashok,
    can you help in how to change permissions of lrwxrwxrwx 1 root : root rmd.log -> /opt/IBM/HTTPServerV8.5/htdocs/logs/RMDLogs/rmd.log
    if i want to have permisions as 770 instead of 777..can i know how to change?? as with chmod the permisions are not changing
    thank you

    1. Hi Ramana,

      Chmod, that’s the option. Actually the link file is just a shortcut to the real file. It has the same permission, even it’s showing 777. Try chmod on a link and check the permission of real file.

      See the example pasted below:

      [root@connect ~]# ll my.cnf
      lrwxrwxrwx. 1 arun root 21 Jul  3 04:20 my.cnf -> /var/lib/mysql/my.cnf
      [root@connect ~]# ll /var/lib/mysql/my.cnf
      -rw-r--r--. 2 root root 702 Jul  3 04:26 /var/lib/mysql/my.cnf
      
      Changing permission of link file. 
      
      [root@connect ~]# chmod 755 my.cnf
      [root@connect ~]# ll my.cnf
      lrwxrwxrwx. 1 arun root 21 Jul  3 04:20 my.cnf -> /var/lib/mysql/my.cnf
      [root@connect ~]#
      [root@connect ~]# ll /var/lib/mysql/my.cnf
      -rwxr-xr-x. 2 root root 702 Jul  3 04:26 /var/lib/mysql/my.cnf
      
      Permission of /var/lib/mysql/my.cnf is now changed to 755
      

Leave a Reply

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