HOW TO CREATE SYMLINK (SYMBOLIC LINK) AND HARDLINK IN LINUX
Introduction
In this article we are going to discuss on Link in linux. There are two types of links are available in linux. Onle is called as Symlink (also known as Symbolic Link) and other one is Hard Link.
Before I explain you what is Symlink and hardlink let’s first Understand a Harddisk related term i.e. Inode. Inode is a part of harddisk 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 harddisk 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
Also Read :
- BEST YUM COMMAND WITH EXAMPLES A PACKAGE MANAGER IN RHEL/CENTOS/FEDORA
- BEST RPM COMMAND ( REDHAT PACKAGE MANAGER ) WITH EXAMPLES IN LINUX
- BEST ZIP COMMAND WITH EXAMPLES IN LINUX
Now Let’s Understand what is Symlink (Symbolic Link) and Hardlink :
Explaining Symlink (Symbolic Link)
Symlink (Symbolic Link): Symlink is nothing otherthen a Shortcut Like we creates Shortcut in Windows Operating System for any Directory or Application. Symlink is linked to a actual or original file which is contains the data. and if the Original file (Source File) got deleted then the Symlink will get unuseable. 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 effect enything. In case of symlink, all the links have different Inode Number. we can create 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 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 Elinuxbook
I am going to create a Symlink of test.txt using ln -s command. The syntax to create symlink is look 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 Elinuxbook
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 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 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 is become 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:53test1.txt->test.txt
Now as the Source file is 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]#
Explaining Hardlink
Hardlink : Hardlink is nothing but a same file with different name. If we delete the Source file then it will doesn’t effect on destination file and also if we delete the destination file it will doesn’t effect on Source file as In case of hardlink all links are Independent and contains same Inode Number. we can identify hardlink by number of links that is connected to actual file and also to hardlinks using command ls -li. we can create hardlink for file only and cannot create hardlink of a directory also Hardlink cannot cross Filesystem.
Let’s domenstrate 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 Elinuxbook.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 same Inode Number and there is a changes 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 more 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 the output below.
[root@localhost data]# cat file2.txt Welcome to Elinuxbook.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 delete 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 the any file will doesn’t effect the other files as all files are independent.
[root@localhost data]# cat file2.txt Welcome to Elinuxbook.com
If you found this article then Like Us, Follow Us or Subscribe us for more Latest Linux Tutorials, guides and News directly in to your Email ID.