Monday, October 27, 2014

difference between soft links and hard links in linux

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 the other. When you delete hard link nothing will happen to the other. Hard links can't cross file systems.

Soft Link is actual link to the original file. These Links will have a different Inodes value. Soft link points to the original file so If original file is deleted 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 inode, Hard 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 shows number of links.
3. Links have actual file contents
4. Removing any link, just reduces the link count, but doesn't affect other links.
5. You cannot create a hard link for a directory.
6 If original file is removed then the link will still show you the content 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 original file.
3. Soft Link contains the path for original file and not the contents.
4. Removing soft link doesn't affect anything but removing original file, the link becomes "dangling" link which 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 hard and then move into it and create new file. Simply follow below steps.

Hard links

$ mkdir hard
$ cd hard
$ touch file1

Now, create a hard link to file1. Name the hard link file2.

$ ln file1 file2

Display inodes for both files using ‘I’ argument of the ls command.

$ ls -il file1 file2

This is what you get:
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 file1
1482256 -rw-r--r-- 2 bruno bruno 21 May 5 15:55 file2

From the output you can notice that both file1 and file2 have the same inode number (1482256). Also both files have the same file permissions and the same size.

Now Remove the original file1

$ rm file1

After removing hard link just have a look at the content of the "link" file2.

$ cat file2

You will still be able to see the content of the file.

Symbolic links

Create soft link for the file file2.
$ ln -s file2 file3

Display inodes for both using i argument of ls command.

$ ls -il file2 file3

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 got a "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 file2
$ cat file3

Now remove the original file:

$ rm file2

And then check the Test directory:

$ ls 

It will still display symbolic link file3 but if you try to list the contents It will tell you that there is no such file or directory.

$ cat file3

Now you know about some of the key differences between hard links and soft links to make it easier to access files and run programs.

No comments:

Post a Comment