Pages

Thursday, June 21, 2012

Hard link vs soft link

Share it Please

In this article, we will see what are hard links, symbolic links, and their differences and how to create those.
Everything in linux is a file. A file in linux contains 2 parts, data part and the file name part.
The Data part is associated with an “Inode”. So what is an Inode?
Inode:  An inode stores basic information about a regular file, directory, or other file system object. Every file in a linux file system contains the following attributes,
  • File type (executable, block special etc)
  • Permissions (read, write etc)
  • Owner
  • Group
  • File Size
  • File access, change and modification time (remember UNIX or Linux never stores file (Creation time)
  • File deletion time
  • Number of links (soft/hard)
    Extended attribute such as append only or no one can delete file including root user
  • Access Control List (ACLs)
All the above information is stored in the Inode. Inode can be obtained using
vx111a:root-~ $ ls -i diff.out
131154  diff.out
You can also use stat command to find Inode like
vx111a:root-~ $ stat diff.out
File: `diff.out'
Size: 610             Blocks: 8          IO Block: 4096   regular file
Device: fd09h/64777d    Inode: 131154      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 7287/ djbs003)   Gid: ( 1566/  jbsgrp)
Access: 2012-02-11 18:22:29.000000000 -0600
Modify: 2011-06-22 10:15:57.000000000 -0500
Change: 2011-06-22 10:15:57.000000000 -0500
Note: If files created with names containing control characters like “*sam”, then these files cannot be removed .These file can only be removed using Inode number like
find . -inum <Inode Number> -exec rm -f {} \;
Now coming to the Links, as we said the data part is associated with an Inode number. The Inode number caries the map of the where the data is , the permission and additional information(see above).

The other part is the File Name part which contains information about the File Name and Inode Information.
Now More than one file can refer to the same inode number


Then the 2 files are said to be hard linked.
Now there are other files in which the Inode carries a path to another file. The operating system recognizes the data as a path and transfers calls likes read, write to the other file instead of accessing the data part in the current file.
This Special file is called symbolic link file or sym link file.
The File Name Part of the current file is stored in the file name part of its own and also in another file called directory file. This directory file contains file name parts of all files that are going to be created. This directory file is nothing but a array of all file name parts of different files created in that directory.
Now, the filename part of the file is stored in a special file of its own along with the filename parts of other files; this special file is called a directory. The directory, as a file, is just an array of filename parts of other files.
When a directory is built, it is initially populated with the filename parts of two special files: the '.' and '..' files. The filename part for the '.' file is populated with the inode# of the directory file in which the entry has been made; '.' is a hardlink to the file that implements the current directory.
The filename part for the '..' file is populated with the inode# of the directory file that contains the filename part of the current directory file. '..' is a hardlink to the file that implements the immediate parent of the current directory.
The 'ln' command knows how to build hardlinks and softlinks; the 'mkdir' command knows how to build directories (the OS takes care of the above hardlinks).
There are restrictions on what can be hardlinked (both links must reside on the same filesystem, the source file must exist, etc.) that are not applicable to softlinks (source and target can be on seperate file systems, source does not have to exist, etc.). OTOH, softlinks have other restrictions not shared by hardlinks (additional I/O necessary to complete file access, additional storage taken up by softlink file's data, etc.)
Happy Learning