The difference between hard link and soft link is somewhat confusing. It confuses the beginning and sometimes experienced system administrators as well. In this article I’m trying to explain the difference between the two theoretically and practically as well. Before starting I’ll layout foundation by just explaining some simple necessary terms.
What is Link Anyways
The link is nothing but just the shortcut to a file or directory. The ln command does the purpose of creating links to files or directories. Now from I’ll use the term file, even for directories. After all unix treats everything as a file in OS environment.
What is Inode Then
Inode is a data structure which stores the information of files, directories, devices, filesystem objects etc. So, for any file existing in a unix system, an inode will be there. Any file can be recognized by its inode number. I’ll tell you how to see inode number of files. Unix deals with any objects like files, directories, devices, filesystem objects with the help of inodes.
What is There in An Inode
Following are the contents of an inode for every file:
- User and group ownership.
- User permissions on files like read, write and execute.
- Type of file.
- File length in bytes.
- Number of links to file.
- Access privileges.
- Time of last access and last modification.
- Time of last inode change.
- Pointers to the file’s data.
- File allocation information.
- Direct and indirect extent information.
How To See The Inode Number Associated With File
To see the inode number, you need to issue ls command with -i option.
Below is the usage:
wiw_labs: $ ls -li maps maps_soft_link maps_hard_link
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps_hard_link
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> maps
What Do I Have To Do With Inodes
When a link is created to a file, we have two options:
- Assigning fresh inode to the new link.
- Assigning inode of the original file to the link, for which we are creating a link.
What is A Soft Link
When we create a soft link, a fresh inode is assigned to the link. So the original file and soft link will have different inode numbers. The new inode points to the original inode. So, whatever data we access through the soft link, is retrieved from the original file through original inode. That means if we delete the original file, the soft link won’t be able to show you the data of file, the original inode is freed up as a result of original file deletion. If still you have doubt, wait for the practical demo.
What is A Hard Link
When we create a hard link, the original inode is assigned to the link. The original file and hard link will be having same inode number. So, imagine we delete the original file. The data is still accessible, because we still have the original inode. It’s not gone anywhere.
Practically Demonstrated Commands
Below I’m pasting the outputs I created after running the commands to show you the demo.
Here I’ve taken three cases:
- Case 1: Original file is deleted and then we cat both soft and hard links.
- Case 2: Hard link is deleted and we cat both original file and soft link.
- Case 3: Soft link is deleted and we cat both original file and hard link.
Original file is maps and its contents as:
A quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.Creating links… Case 1
wiw_labs: $ ln -s maps maps_soft_link
wiw_labs: $ ln maps maps_hard_linkwiw_labs: $ ls -li maps maps_soft_link maps_hard_link
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps_hard_link
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> mapsDeleting original file..
wiw_labs: $ rm mapswiw_labs: $ ls -li maps_soft_link maps_hard_link
64232 -rw-r–r– 1 lab_user lab_user 91 May 15 06:09 maps_hard_link
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> mapscatting maps_soft_link
wiw_labs: $ cat maps_soft_linkcat: cannot open maps_soft_link
catting maps_hard_link
wiw_labs: $ cat maps_hard_linkA quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.Creating links… Case 2
wiw_labs: $ ln -s maps maps_soft_link
wiw_labs: $ ln maps maps_hard_linkwiw_labs: $ ls -li maps maps_soft_link maps_hard_link
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps_hard_link
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> mapsDeleting hard link…
wiw_labs: $ rm maps_hard_linkwiw_labs: $ ls -li maps_soft_link maps
64232 -rw-r–r– 1 lab_user lab_user 91 May 15 06:09 maps
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> mapscatting maps_soft_link
wiw_labs: $ cat maps_soft_linkA quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.catting maps
wiw_labs: $ cat mapsA quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.Creating links… Case 3
wiw_labs: $ ln -s maps maps_soft_link
wiw_labs: $ ln maps maps_hard_linkwiw_labs: $ ls -li maps maps_soft_link maps_hard_link
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps_hard_link
64234 lrwxrwxrwx 1 lab_user lab_user 4 May 15 06:09 maps_soft_link -> mapsDeleting soft link…
wiw_labs: $ rm maps_soft_linkwiw_labs: $ ls -li maps maps_hard_link
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps
64232 -rw-r–r– 2 lab_user lab_user 91 May 15 06:09 maps_hard_linkcatting maps_hard_link
wiw_labs: $ cat maps_hard_linkA quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.catting maps
wiw_labs: $ cat mapsA quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.
very nice explanation…..