Hard link and soft link with examples in Linux / Unix

Hard link and soft link with examples in Linux / Unix

Their are two link files in linux they are Soft link and Hard link.These two types of links will create a duplicate file with same content in the original file. If any update occurs in soft link’s and Hard link’s original file it will reflect in the duplicate file and vise verse.

 

Hard link and soft link with examples in Linux / Unix

 

Let’s learn some thing about soft link.

Soft link:

In linux Soft link can also said to symlink.Soft link is similar to our windows shortcut If you create a soft link it simply creating a pointer to the existing file. In soft link if the duplicate file is removed it can be recovered from source file but if the source file is removed it cannot be recovered. If you are creating a soft link always have practice of creating it with absolute path or full path of the file. Soft link file will always starts with ‘l’, you can verify it with “ls -l command”. Soft link file will have its own inode number. Soft link can be applied between different linux file system but in hard link it cannot be accomplished. Soft link will always have “777” permission

How to create a Soft link in linux / Unix

Syntax:

#ln -s <Source> <Destination>

Example:

#cp /etc/passwd /root/Desktop/testing/

#ln -s /root/Desktop/testing/passwd  /root/Desktop/testing/softlink

How to remove a soft link in Linux / Unix

#rm -rf <Soft link file>

 

Hard link and soft link with examples in Linux / Unix

 

Hard Link:

If you create a hard link, you are creating another pointer to the data location on disk. That is data location is one point in the Hard disk but it can be accessed from multiple files.  Since hard link is a direct reference to the filesystem, It cannot be hard link across different filesystems.In hard link if the duplicate file is removed we can use source file and even if the source file is deleted we can use the duplicate file. Our data will be safe unless until you delete both the source and Hard link file. Hard link file will have same file permission of the original file.

How to create hard link in Linux / Unix

Syntax:

#ln <Source> <Destination>

Example:

#cp /etc/shadow /root/Desktop/testing/

#ln /root/Desktop/testing/shadow /root/Desktop/testing/hardlink1
#ln /root/Desktop/testing/shadow /root/Desktop/testing/hardlink2
#ln /root/Desktop/testing/shadow /root/Desktop/testing/hardlink3
#ln /root/Desktop/testing/shadow /root/Desktop/testing/hardlink4

 

Hard link and soft link with examples in Linux / Unix




Leave a Reply

Your email address will not be published. Required fields are marked *