Skip to main content

Soft link vs Hard link in unix

 

Soft Link  Vs Hard Link

 

 

A symbolic or soft link is  same as shortcut on windows, it is an actual link to the original file, whereas a hard link on the other hand is a mirror copy of the original file.

If you delete the original file, the soft link may appear in a long listing but has no value, because it points to a non-existent file.

But in the case of hard link, it is the other way around. If you delete the original file, the hard link can still have the data of the original file.  Because hard link is like a mirror copy of the original file.

In a nutshell, a soft link

·         can cross the file system,

·         allows you to link between directories,

·         has different inodes number and file permissions than original file,

·         permissions will not be updated,

·         has only the path of the original file, not the contents.

A hard Link

·         can’t cross the file system boundaries,

·         can’t link directories,

·         has the same inodes number and permissions of original file,

·         permissions will be updated if we change the permissions of source file,

·         has the actual contents of original file, so that you still can view the contents, even if the original file moved or removed.

To understand the soft link and hard link lets follow an example.

Creating Soft, or Symbolic Link

Let us create an empty directory called “test”.

 

$ mkdir test

Change to the “test” directory:

 

$ cd test

Now, create a new file called source.file with some data as shown below.

$ echo "Welcome to OSTechNix" >source.file

Let us view the data of the source.file.

$ cat source.file
Welcome to OSTechNix

 

Well, the source.file has been created.

Now, create the a symbolic or soft link to the source.file.

To do so, run:

$ ln -s source.file softlink.file

Let us view the data of softlink.file.

$ cat softlink.file

 

As you see in the above output, softlink.file displays the same data as source.file.

Let us check the inodes and permissions of softlink.file and source.file.

$ ls -lia

Sample output:

total 12
15745326 drwxr-xr-x 2 sk users 4096 Dec 13 14:55 .
15728642 drwx------ 49 sk users 4096 Dec 13 14:50 ..
15746561 lrwxrwxrwx 1 sk users 11 Dec 13 14:55 softlink.file -> source.file
15746185 -rw-r--r-- 1 sk users 21 Dec 13 14:53 source.file

 

As we see in the above screenshot, even though the softlink.file has same contents as source.file, the inodes number (15746561 vs 15746185) and file permissions (lrwxrwxrwx vs -rw-r–r–) are different. Hence, it is proved that soft link doesn’t share the same inodes number and permissions of original file.

Now, remove the original file (i.e source.file) and see what happens.

$ rm source.file

Check output of softlink.file using command:

$ cat softlink.file

Sample output:

cat: softlink.file: No such file or directory

 

As you see above, there is no such file or directory called softlink.file after we we removed the original file (i.e source.file). So, now we understand that soft link is just a link that points to the original file. The softlink is like a shortcut to a file. If you remove the file, the shortcut is useless.

As you already know, if you remove the soft link, the original file will still present.

Creating Hard Link

Create a file called source.file with some contents as shown below.

$ echo "Welcome to OSTechNix" >source.file

Let us verify the contents of the file.

$ cat source.file
Welcome to OSTechNix

source.file has been created now.

Now, let us create the hard link to the source.file as shown below.

$ ln source.file hardlink.file

 

Check the contents of hardlink.file.

$ cat hardlink.file
Welcome to OSTechNix

You see the hardlink.file displays the same data as source.file.

Let us check the inodes and permissions of softlink.file and source.file.

$ ls -lia

Sample output:

total 16
15745326 drwxr-xr-x 2 sk users 4096 Dec 13 15:22 .
15728642 drwx------ 49 sk users 4096 Dec 13 14:50 ..
15745555 -rw-r--r-- 2 sk users 21 Dec 13 15:20 hardlink.file
15745555 -rw-r--r-- 2 sk users 21 Dec 13 15:20 source.file

 

Now, we see that both hardlink.file and source.file have the same the inodes number (15745555) and file permissions (-rw-r–r–). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.

Note: If we change the permissions on source.file, the same permission will be applied to the hardlink.file as well.

Now, remove the original file (i.e source.file) and see what happens.

$ rm source.file

Check contents of hardlink.file using command:

$ cat hardlink.file

Sample output:

 

As you see above, even if I deleted the source file, I can view contents of the hardlink.file. Hence, it is proved that Hard link shares the same inodes number, the permissions and data of the original file.

That’s all for now. Hope you got a basic idea about symbolic or soft link and hard link. If you find this guide useful, share it on your social, professional networks and support us.

 

Comments

Unknown said…
Usefull and relavent points easy to understand
Karthik gowda k said…
very helpfull made this complex topic too easy and simple to learn
Unknown said…
Wow good explanation
Very simple and easy to understand
Thank you for This
Keep it up 😊
rishav said…
Hope to get more such good explanation in future.
Easy explanation helps.
Sowmya Reddy said…
great one to refer..this made complex concepts to understand easily. Hoping for many more such blogs.
Unknown said…
though it was very helpful and very easy to understand .
The explanation did very nice and understandable.
one good point I liked is for everything you gave an example which helps to understand easily.
Manish said…
Precise and informative. Just add FAQ related to topic at the end
It was easy to understand and informative.
Ankur kumar said…
Very helpful and Easy to understand.
Unknown said…
Smoothly explained
Mukul Mahajan said…
Very helpfull to understand difference between hardlink and softlink with demo.
Very Informative and Helpful. Easy to understand as well. With examples it was a game changer. Loved it.
Prateek Kumar said…
Very Useful post
Made it simple and easy
G Karibasava said…
Topic is very simple to explain and also to understand. Nice whatever you thought me.
AJAY said…
Thanks for the simple contents. It is easy to understand and learn.
saptarshi said…
really helpful way to learn hard and soft links
saptarshi said…
This comment has been removed by the author.
Unknown said…
Very informative and simple to understand
amir said…
Really helpful. Simple and easy to understand.
Unknown said…
Explained in very simple and easy way.you made this topic very easy to understand and learn.
Thank you for your explanation, help and support. keep blogging about the topics sir.
Arunangshu Sen said…
Your explanation is right on the money and crystal-clear. Thank you so much. Kindly continue with all the good work.
Tapas Mahato said…
The explanation is very simple and easy to understand.
Guruprasad p said…
This comment has been removed by the author.
The content is pretty simple,straight and easy to understand.I just followed those commands and now I know how to create a softlink and hardlink.
Pranav said…
The content was easy to understand and learn, provided with the examples here helps us to know how to implement the concepts clearly.
Guruprasad p said…
Ajay,

Nice info and amazing write-up,
it found helpful for me,
Thank u and i am waiting for more blogs from u
Unknown said…
The content is simple and easy to understand.
Sourav Nandy said…
great explanation...
and nicely talk about the implementations and examples.
Shiva Surabhi said…
Looks intresting very helpful. Thanks for the blog.

Popular posts from this blog

RDBMS MINI PROJECT

  Capgemini class room training.   RDBMS MINI PROJECT ( SPRINT ) LIBRARY MANAGEMENT SYSTEM   Table of Contents Serial No. Topic Name Content Page No. 1.   Introduction 1.1 Setup checklist for mini project 3     1.2 Instructions 3 2.   Problem statement   2.1 Objective 4     2.2 Abstract of the project 4     2.3 Functional components of the project 4     2.4 Technology used 5 3.   Implementation in RDBMS LOT 3.1 Guidelines on the functionality to be built 6 4.   Evaluation 4.1 Evaluation 7   ...

PL / SQL practice programs

  PL / SQL  practice programs 1. Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING   BEGIN   DBMS_OUTPUT.PUT_LINE('WELCOME   TO   PL/SQL   PROGRAMMING'); END; /   2. Write a program to print the numbers from 1 to 100   DECLARE N NUMBER(3):=1; V VARCHAR2(1000); BEGIN WHILE N <=1000 LOOP V:=V||''||N; N:=N+1; END LOOP; DBMS_OUTPUT.PUT_LINE(V); END; / 3. write a program to print the even numbers from 1 to 100 DECLARE N NUMBER(3):=0; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 4. Write a program to print the odd numbers from 1 to 100 DECLARE N NUMBER(3):=1; BEGIN WHILE N <=100 LOOP N:=N+2; DBMS_OUTPUT.PUT_LINE(N); END LOOP; END; / 5. write a program for multiplication table DECLARE A NUMBER(2):=&A; B   NUMBER(2):=1; C ...

sample tables

  --sample tables DROP TABLE EMP; DROP TABLE DEPT; DROP TABLE BONUS; DROP TABLE SALGRADE; DROP TABLE DUMMY;   CREATE TABLE EMP        (EMPNO NUMBER(4) NOT NULL,         ENAME VARCHAR2(10),         JOB VARCHAR2(9),         MGR NUMBER(4),         HIREDATE DATE,         SAL NUMBER(7, 2),         COMM NUMBER(7, 2),         DEPTNO NUMBER(2));   INSERT INTO EMP VALUES         (7369, 'SMITH',   'CLERK',      7902,         TO_DATE('17-DEC-1980', 'DD-MON-YYYY'),   800, NULL, 20); INSERT INTO EMP VALUES         (7499, 'ALLEN',   'SALESMAN',   7698,    ...