Posts Tagged ‘Linux’

What is The Difference Between Hard and Soft Link

Saturday, May 16th, 2009

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:

  1. User and group ownership.
  2. User permissions on files like read, write and execute.
  3. Type of file.
  4. File length in bytes.
  5. Number of links to file.
  6. Access privileges.
  7. Time of last access and last modification.
  8. Time of last inode change.
  9. Pointers to the file’s data.
  10. File allocation information.
  11. 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:

  1. Assigning fresh inode to the new link.
  2. 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:

  1. Case 1: Original file is deleted and then we cat both soft and hard links.
  2. Case 2: Hard link is deleted and we cat both original file and soft link.
  3. 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_link

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

Deleting original file..
wiw_labs: $ rm maps

wiw_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 -> maps

catting maps_soft_link
wiw_labs: $ cat maps_soft_link

cat: cannot open maps_soft_link

catting maps_hard_link
wiw_labs: $ cat maps_hard_link

A 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_link

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

Deleting hard link…
wiw_labs: $ rm maps_hard_link

wiw_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 -> maps

catting maps_soft_link
wiw_labs: $ cat maps_soft_link

A quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.

catting maps
wiw_labs: $ cat maps

A 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_link

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

Deleting soft link…
wiw_labs: $ rm maps_soft_link

wiw_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_link

catting maps_hard_link
wiw_labs: $ cat maps_hard_link

A quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.

catting maps
wiw_labs: $ cat maps

A quick brown fox jumps over the lazy dog.
This line has all english alphabets
from A – Z.

Tags: , , , ,
Posted in Unix | No Comments »