In this tutorial, we will see the difference between hard link and soft link in Linux. Provided a table with hard link vs soft link with some examples to make it clear.
Hard Link acts like a mirror copy of the original file. These links share the same inodes. Changes made to the original or hard linked file will reflect in the other. When you delete Hard Link nothing will happen to the other file. Hard links can't cross file systems.
Soft Link is an actual link to the original file. These Links will have a different Inodes value. Soft link points to the original file so if the original file is deleted then the soft link fails. If you delete the Soft Link, nothing will happen to file. The reason for this is, the actual file or directory’s inode is different from the "soft link" created file's inodes. Soft links can cross file systems.
What are Hard Links
1. Hard Links have same inodes number.
2. ls -l command shows all the links with the link column showing the number of links.
3. Links have actual file contents
4. Removing any link, just reduces the link count but doesn't affect the other links.
5. You cannot create a Hard Link for a directory.
6. Even if the original file is removed, the link will still show you the contents of the file.
What are Soft Links
1. Soft Links have different inodes numbers.
2. ls -l command shows all links with second column value 1 and the link points to the original file.
3. Soft Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but when the original file is removed, the link becomes a 'dangling' link that points to nonexistent file.
5. A Soft Link can link to a directory.
Let us try to see some experimental differences. Make a new directory called 'Test' and then move into it and create a new file. Simply follow the steps given below:
Lets first create a "Test" directory and inside we create a new file "sample1".
$ mkdir Test $ cd Test $ touch sample1
Now, create a hard link to sample1. Name the hard link sample2.
$ ln sample1 sample2
Display inodes for both files using
i argument of the ls command.
$ ls -il sample1 sample2
This is what you will get as output
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 sample1 1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 sample2
From the output, you can notice that both sample1 and sample2 have the same inode number (1482256). Also, both files have the same file permissions and the same size.
Now Remove the original sample1
$ rm sample1
After removing Hard Link just have a look at the content of the "link" sample2.
$ cat sample2
You will still be able to see the contents of the file.
Let's create a soft link for the file sample2 using below command.
$ ln -s sample2 sample3
Display inodes for both using
i argument of ls command.
$ ls -il sample2 sample3
This is what you'll get:
1482256 -rw-r--r-- 1 bruno bruno 21 May 5 15:55 FileB 1482226 lrwxrwxrwx 1 bruno bruno 5 May 5 16:22 FileC -> FileB
From the output, you can notice that the inodes are different and the symbolic link has an "l" before the rwxrwxrwx. The permissions are different for the link and the original file because it is just a symbolic link.
Now list the contents:
$ cat sample2 $ cat sample3
Now remove the original file
$ rm sample2
And then check the Test directory:
It will still display symbolic link sample3 but if you try to list the contents, it will tell you that there is no such file or directory.
$ cat sample3
Now you know about some of the key differences between Hard Links and Soft Links that will make it easier for you to access files and run programs.