Site icon UnixArena

Unix File’s – hard link and soft link

In Unix, links allow more than one file name to refer to the same file.In windows terminology,we use to call it as  shortcut. (soft link).Link will not consume any disk space.It will just refer the source file.In Solaris,if you want to see soft links just go to /etc/rc2.d or /etc/rc3.d directory.You can find most links on those directories and it will point to /etc/init.d files.For an example for hardlink is /etc/inet/hosts & /etc/hosts.

syntax to create a hardlink:
# ln soure_file new_file

Hard Link:
This type of link files will share the same inode number with original files.In other words, “file” and hard link will share the physical data.

bash-3.00# ln /var/tmp/brerr /var/brerr_hardlink 
bash-3.00# ls -l /var/brerr_hardlink
-rw-r--r-- 2 root root 304 Jul 17 14:12 /var/brerr_hardlink
bash-3.00# file /var/brerr_hardlink
/var/brerr_hardlink: ascii text

In the below example, i have created hard link “brerr_hardlink” for file “brerr” which is located under /var/tmp.
Both files are reading the same data location.

bash-3.00# cat /var/tmp/brerr
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.

Editable data also available since its pointing to same inode. 

bash-3.00# vi /var/tmp/brerr
"/var/tmp/brerr" 8 lines, 304 characters
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges
:wq!

bash-3.00#

bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges

If you remove the original file ,it will just remove the file name not inode.So still you can access to the hard link file . 

bash-3.00# rm /var/tmp/brerr

bash-3.00# cat /var/brerr_hardlink
Thu Jun 28 14:20:38 IST 2012.
Java Accessibility Bridge for GNOME loaded.
edited by linges

Limitations of hard link.
  • You can not create hard link across the filesystem. 

Symbolic (soft) link:
Soft link is nothing but shortcut in windows terminology .If you remove the source file , you soft link will become invalid.

syntax:

# ln -s soure_file new_file
Here i am creating soft link for file "VRTS6.0.tar" in /opt with name of "tar_file"
bash-3.00# ln -s /var/tmp/VRTS6.0.tar /opt/tar_file
bash-3.00# file /opt/tar_file
/opt/tar_file: USTAR tar archive
bash-3.00# ls -l /opt/tar_file
lrwxrwxrwx 1 root root 20 Jul 25 18:16 /opt/tar_file -> /var/tmp/VRTS6.0.tar


Advantage over hard-link:

  • You can create soft link across the filesystem .

Thank you for reading this article.Please leave a comment if you have any doubt ,i will get back to you as soon as possible.

Exit mobile version