System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

differ hard link and soft link

Abubakkar
Frequent Contributor

differ hard link and soft link

differ hard link and soft link , how to create links.
2 REPLIES
Matti_Kurkela
Honored Contributor

Re: differ hard link and soft link

The "ln" command can be used to create links. It works like "cp" but creates a link instead of a copy.

"ln " creates a hard link.

"ln -s " creates a soft link.


Hard links work only within a single filesystem: you cannot make a hard link point from one filesystem to another filesystem.

A hard link is indistinguishable from the original file: the same content can be accessed using both filenames. If you have some data in "file-a" and hard-link it as "file-b", and then delete "file-a", the data will still exist as "file-b".

echo "*some data*" > file-a
ln file-a file-b
rm file-a
cat file-b
*some data*

A soft link is just a pointer, telling the system "look at this other file instead". A soft link can point to a different (mounted) filesystem. If you delete the original file that is the target of a soft link, the data will be lost.

echo "*some data*" > file-c
ln file-c file-d
rm file-c
cat file-d
cat: file-d: No such file or directory

ls -l file-d
lrwxrwxrwx 1 user group 6 May 12 15:51 file-d -> file-c

"file-d" exists, but because it's a soft link that points to a non-existent "file-c", the error message says the file does not exist. (This is called a "dangling link".)

If someone later creates "file-c" again, the soft link will start working again.

MK
MK
Viktor Balogh
Honored Contributor

Re: differ hard link and soft link

nice explanation Matti. however, I found a typo, a soft link is made with the "-s" option:

instead this:
> ln file-c file-d

here is the correct one:
ln -s file-c file-d
****
Unix operates with beer.