Monday 20 January 2014

Hardlink and Softlink Analysis

Links are special files that acts as a reference to another file or directory.

There are two types of link hardlink and softlink.

Softlink: A softlink is also known as symlink or symbolic link.
Hardlink: It specifies the specific location of physical data.

Don't get confused.

Lets looks each and every property of both hardlink and softlink with an example:

SOFTLINK:
=========

1) Creating softlink:

Command: ln -s /path/to/orginal/file /path/to/link/file

Example: I am creating a file "softtest" under user1 and creating a link for it as "softtestlink" under user2. Now, analyze the results below:
 =====
root@cpanel1 [/home/user1/public_html]# ll softtest
-rw-r--r-- 1 root root 0 Jan 20 05:05 softtest
root@cpanel1 [/home/user1/public_html]# ln -s /home/user1/public_html/softtest  /home/user2/softtestlink
 ====

Now, navigate through "/home/user2", you will be able to see the link:
======
root@cpanel1 [/home/user2]# pwd
/home/user2
root@cpanel1 [/home/user2]# ll softtestlink
lrwxrwxrwx 1 root root 31 Jan 20 05:05 softtestlink -> /home/user2/public_html/softtest
root@cpanel1 [/home/user2]#
 ======

2) In symlink inode will be different:

Lets see with examples:

31166346 is the inode value of orginal file:
=====
root@cpanel1 [/home/user2/public_html]# ls -il softtest
31166346 -rw-r--r-- 1 root root 0 Jan 20 05:08 softtest
root@cpanel1 [/home/user2/public_html]#
=====

32243719 is the inode value of softlink:
=====
root@cpanel1 [/home/user2]# ls -il softtestlink
32243719 lrwxrwxrwx 1 root root 31 Jan 20 05:05 softtestlink -> /home/user1/public_html/softtest
root@cpanel1 [/home/user2]#
=====

In short inodes are different in case of symlink.

3) Deleting the original file results in loss of data but link exists, while deleting the  link will still make sure that data is present.
=====
root@cpanel1 [/home/user2]# ll softtestlink
lrwxrwxrwx 1 root root 31 Jan 20 05:05 softtestlink -> /home/user1/public_html/softtest
root@cpanel1 [/home/user2]# rm -rf softtestlink
==========
root@cpanel1 [/home/user2# ll softtestlink
/bin/ls: softtestlink: No such file or directory
root@cpanel1 [/home/user2]#
======

But orginal file still exists:
====
root@cpanel1 [/home/user1/public_html]# ll softtest
-rw-r--r-- 1 root root 0 Jan 20 05:08 softtest
root@cpanel1 [/home/user1/public_html]#
====

While when orginal file is deleted "softlink" will be shown as broken.

4) softlink can be created for BOTH DIRECTORIES AND FILES.

HARDLINK:
=======
1) Creating hardlink:

Command:
 
ln /full/path/of/original/file /full/path/of/hard/link/file

2) Hardlink can be created ONLY FOR FILES.

3) Hardlinks will always have same inode. Hence, it CANNOT CROSS FILE SYSTEM. This is because Linux cannot keep same inodes across filesystem.

4) Removing the Hard Linked Files

When you delete a file that is hard linked, you would be still able to access the content of the file until you have the last file which is hard linked to it, as shown in the example below.

Create a sample file.

$ vim test1.txt
Created this file to test the hard link.

Create a hard link to the sample file.

$ ln test1.txt test2.txt

Delete the original file.

$ rm test1.txt

You can still access the original file content by using the hard link you created.

$ cat test2.txt

Created this file to test the hard link.


Practical scenario: Very USEFUL:

Let us assume that you have two partitions – 5GB and 20GB. The first partition does not have too much free space available in it. If a program located on the first partition needs more space (For example, for it’s log file), you can use some of the space from the second partition by creating a link for the log files as shown below.

Consider that partition1 is mounted on /, and partition2 is mounted to /mnt/. Let us assume that the logs that are located on partition1 is running out of space, and you’ve decided to move them to partition2. You can achieve this as shown below.

$ mkdir /mnt/logs

$ cd /logs

$ mv * /mnt/logs

$ cd /; rmdir logs

$ ln -s /mnt/logs logs

REF: http://www.thegeekstuff.com/2010/10/linux-ln-command-examples/

No comments:

Post a Comment

Note: only a member of this blog may post a comment.