Tuesday, April 5, 2022

Linux soft link and hard link

 Soft (Symbolic) Link

This is an actual link to the original file. It is just like a shortcut to the original file. In case the original file is deleted then the soft link will not work.

Hard link

It is the mirror copy of the original file and original file and the hard link file points to the same inode number therefore if any change happens in the original or hard link file it gets reflected on both the files

Soft link Lab

Create a directory called test

 mkdir test

 cd test

Create a source file (source.file)

 echo "Welcome to Linux" > source.file

 cat source.file

Create a soft link file that is pointing to the source file

 ln -s source.file softlink.file

cat softlink.file

cat source.file

 ls -l

Remove the source file and check that you are able to read the link file

  rm source.file

  cat softlink.file

Hard link Lab

Create the source file

 echo "Welcome to Linux hard link example" >source.file

  cat source.file

Create a hard link file.

 ln source.file hardlink.file

 ls -li

cat hardlink.file

Change the permission to any of these files and you will notice that changed permissions are reflected on both the files

 chmod u+x source.file

 ls -li

 chmod u-x hardlink.file

 ls -li

Update any of these file and you will find that the changes are reflected in both the files

  echo "Adding new line" >> source.file

  cat hardlink.file

Delete source file and you will find that hard link file is not deleted.

rm source.file

cat hardlink.file


0 comments:

Post a Comment