How To Create Symlink (Symbolic Link) And Hardlink In Linux

In this article, we are going to discuss on Link in Linux. There are two types of links are available in Linux. One is called as Symlink (also known as Symbolic Link) and another one is Hard Link.

Before I explain to you what is Symlink and hardlink let’s first Understand a Harddisk related term i.e. Inode. An inode is a part of the hard disk which stores file information like Owner of the file, File size, Last modified date and time, copied date and time, etc…When we create a file in the hard disk at that time only system assign’s a unique number to that file is called as Inode Number. We can check inode number of any file by ls -li command. Follow the Sample Output below.

Note: Inode Number is Highlighted in Red Color.

[root@localhost data]# ls -li   # To check Inode Number of a File
total 0
401559 -rw-r--r--. 1 root root 0 Apr  5 10:51 test.txt

Now Let’s Understand what is Symlink (Symbolic Link) and Hardlink :

How To Create Symlink (Symbolic Link) And Hardlink In Linux

How To Create Symlink (Symbolic Link) And Hardlink In Linux

Explaining Symlink (Symbolic Link)

Symlink (Symbolic Link): Symlink is nothing other than a Shortcut Like we create Shortcut in Windows Operating System for any Directory or Application. A symlink is linked to an actual or original file which contains the data. and if the Original file (Source File) got deleted then the Symlink will get unusable. Such file is called as orphaned as the original file to which the symlink was connected is now not exist on the linked path but deleting the symlink will not affect anything. In the case of the symlink, all the links have different Inode Number. we can create a symlink of both file and directory and symlink can cross the file system. we can create symlink using ln -s command. Let’s Understand the Symlink more deeply.

Suppose I have a File named test.txt with some data and Inode Number of the file is 401559. Refer to the sample output below.

Note: Inode Number is Highlighted in Red Color.

[root@localhost data]# ls -li
total 0
401559 -rw-r--r--. 1 root root 0 Apr  5 10:51 test.txt

[root@localhost data]# cat test.txt
Welcome to ITSmarttricks

I am going to create a Symlink of test.txt using ln -s command. The syntax to create symlink is looked like this :

ln -s <Source File> <Destination File>

[root@localhost data]# ln -s test.txt test1.txt   # To Create a Symbolic Link

# As we can see below we can see the content of the test.txt file in test1.txt (Symbolic Link)

[root@localhost data]# cat test1.txt 
Welcome to ITSmarttricks

Now let’s check Inode Number of both files by using ls -li command and another noticeable thing you can found on below output is, in front of  test1.txt (Highlighted in Skyblue Color) the path of the source file is mentioned which means test1.txt is a symlink of the file test.txt.

[root@localhost data]# ls -li
total 0
401562 lrwxrwxrwx. 1 root root 8 Apr  5 10:53 test1.txt -> test.txt
401559 -rw-r--r--. 1 root root 0 Apr  5 10:51 test.txt

As we can see on the above sample output both of the file has different Inode Number. Now let’s delete the source file using rm command and check what happens to symlink.

[root@localhost data]# rm file.txt   # Deleting the Source File
rm: remove regular file `file.txt'? y

After deleting the Source file when you are trying to list the Symlink you will able to see that test1.txt becomes unusable as the source file is now not exist on the path where symlink was connected.

[root@localhost data]# ls -l
total 0
lrwxrwxrwx. 1 root root 8 Apr  5 10:53 test1.txt -> test.txt

Now as the Source file does not exist when you try to see the content of file through symlink i.e. test1.txt you will get below error.

[root@localhost data]# cat test1.txt
cat: test1.txt: No such file or directory
[root@localhost data]#

Also Read – Useful RPM Command With Examples In Linux

Explaining Hardlink

Hardlink: Hardlink is nothing but the same file with a different name. If we delete the Source file then it will don’t affect on destination file and also if we delete the destination file it will don’t affect on Source file as In case of hardlink all links are Independent and contain same Inode Number. we can identify hardlink by the number of links that are connected to the actual file and also to hardlinks using command ls -li. we can create hardlink for the file only and cannot create hardlink of a directory also Hardlink cannot cross Filesystem.

Let’s demonstrate Hardlink Practically so that your concept will get more clear.

Suppose I have a file called file.txt with some content. Here you need to notice two things i.e. Inode Number of the File (Highlighted in Red color) and link to that file (Highlighted in Blue color).

[root@localhost data]# ls -li
total 4
401562 -rw-r--r--. 1 root root 26 Apr  5 11:02 file.txt
[root@localhost data]# cat file.txt 
Welcome to ITSmarttricks.com

Now I am going to create a hardlink of file.txt using ln command. The syntax to create hardlink is :

ln <Source File> <Destination File>

After creating the Hardlink you can see that both of the files has the same Inode Number and there is a change happened in link i.e it increased from 1 to 2. The link number 2 is indicating that we have two files i.e. one is source file and another one is hardlink of the source file.

[root@localhost data]# ln file.txt file1.txt  # Creating Hardlink
[root@localhost data]# ls -li
total 8
401562 -rw-r--r--. 2 root root 26 Apr  5 11:02 file1.txt
401562 -rw-r--r--. 2 root root 26 Apr  5 11:02 file.txt

To make the concept easier let’s create one more hardlink.

After creating another Hardlink for file.txt you will able to see that all three files are having Same Inode Number and link is again increased to 3 as now we have the files with same Inode Number. So with the help of link only you can identify the hardlink.

[root@localhost data]# ln file.txt file2.txt
[root@localhost data]# ls -li
total 12
401562 -rw-r--r--. 3 root root 26 Apr  5 11:02 file1.txt
401562 -rw-r--r--. 3 root root 26 Apr  5 11:02 file2.txt
401562 -rw-r--r--. 3 root root 26 Apr  5 11:02 file.txt

Now let’s check the content of the file by using any one of the hardlink. Refer to the output below.

[root@localhost data]# cat file2.txt
Welcome to ITSmarttricks.com

Now let’s delete the Source File and check what changes happening.

[root@localhost data]# rm file.txt
rm: remove regular file `file.txt'? y
[root@localhost data]# ls -li
total 8
401562 -rw-r--r--. 2 root root 26 Apr  5 11:02 file1.txt
401562 -rw-r--r--. 2 root root 26 Apr  5 11:02 file2.txt

After deleting the source file now we have two files with same Inode Number and the link number is now decreased to 2.

And also you can see the content of the file, Refer the output below. So in case of Hardlink deleting any file will don’t affect the other files as all files are independent.

[root@localhost data]# cat file2.txt
Welcome to ITSmarttricks.com

That’s all, In this article, we have explained How To Create Symlink (Symbolic Link) And Hardlink In Linux. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:

Mangesh Dhulap

This is Mangesh Dhulap the Founder and Editor of IT SMART TRICKS have 6+ years of Industrial Experience. We expect from our visitors to like, share, and comment on our posts.

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.