Table of Contents
What is Soft Link And Hard Link In Linux?
A symbolic or soft link is an actual link to the original file, whereas a hard link is a mirror copy of the original file. If you delete the original file, the soft link has no value, because it points to a non-existent file.
But in the case of hard link, it is entirely opposite. Even if you delete the original file, the hard link will still has the data of the original file. Because hard link acts as 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 inode 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 (i.e. A hardlink can only work on the same filesystem),
- can't link directories,
- has the same inode 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.
Still don't get it? Well, allow me to show you some practical examples.
How to create Soft Link 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 compare the data of both source.file
and softlink.file
.
$ cat source.file Welcome to OSTechNix
$ cat softlink.file Welcome to OSTechNix
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 11665675 drwxrwxr-x 2 sk sk 4096 Oct 17 11:39 . 4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 .. 11665731 lrwxrwxrwx 1 sk sk 11 Oct 17 11:39 softlink.file -> source.file 11665692 -rw-rw-r-- 1 sk sk 21 Oct 17 11:39 source.file
As we see in the above screenshot, the inode number (11665731
vs 11665692
) and file permissions (lrwxrwxrwx
vs -rw-r--r--
) are different, even though the softlink.file
has same contents as source.file
. Hence, it is proved that soft link don't share the same inode number and permissions of original file.
Now, remove the original file (i.e source.file
) and see what happens.
$ rm source.file
Check contents of the 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 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 be available.
How to create 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
The 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 inode and permissions of hardlink.file
and source.file
.
$ ls -lia
Sample output:
total 16 11665675 drwxrwxr-x 2 sk sk 4096 Oct 17 11:58 . 4325378 drwxr-xr-x 37 sk sk 4096 Oct 17 11:39 .. 11665692 -rw-rw-r-- 2 sk sk 21 Oct 17 11:57 hardlink.file 11665692 -rw-rw-r-- 2 sk sk 21 Oct 17 11:57 source.file
Now, we see that both hardlink.file
and source.file
have the same the inodes number (11665692
) and file permissions (-rw-r--r--
). Hence, it is proved that hard link file shares the same inodes number and permissions of original file.
Heads Up: 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 inode number, the permissions and data of the original file.
So, what is the difference between Hard link and the normal copied file?
You might be wondering why would we create a hard link while we can easily copy/paste the original file? Creating a hard link to a file is different than copying it.
If you copy a file, it will just duplicate the content. So if you modify the content of a one file (either original or hard link), it has no effect on the other one.
However if you create a hard link to a file and change the content of either of the files, the change will be seen on both.
Let us have a look at the source.file.
$ cat source.file Welcome to OSTechNix
The source file has a single line that says - Welcome to OSTechNix.
Append a new line, for example "Welcome to Linux" in source.file
or hardlink.file
.
$ echo "Welcome to Linux" >>source.file
Now check contents of both files.
$ cat hardlink.file Welcome to OSTechNix Welcome to Linux
$ cat source.file Welcome to OSTechNix Welcome to Linux
See? The changes we just made on source.file
are updated in both files. Meaning - both files (source and hard link) synchronizes.
Whatever changes you do in any file will be reflected on other one. If you normally copy/paste the file, you will not see any new changes in other file.
For more details, check the man pages.
$ man ln
Conclusion
In this guide, we have discussed what is soft link and hard link in Linux, how to create softlink and hardlink with example commands and finally we explained the difference between hardlink and normal copied file.
Hope you got a basic idea of how to use symbolic or soft link and hard link in Linux.
34 comments
Under the “Creating Hard Link” section, thanks to change the word “softlink.file” into “hardlink.file” instead for the statement=>”Let us check the inode and permissions of softlink.file and source.file.” as I believe it was a typo error.
Thanks 🙂
Good catch. Fixed it now. Thank you.
Excllent explanation and very usefull content.
Thank you
good explanation. Easily understandable.
Nice explanation?? It’s not !
Phrasing is confusing.
Hard link is not a mirror copy of original file. Hard link is more like link for the chunk (frame) of data. Hence creating hard link is NOT doubling size. That’s the most important point here. Well, and soft link is a link to the file, NOT link to data on harddrive. And as a result diff. inodes (data block location or data pointers) in case of soft links and identical inodes in case of hard links.
Thanks, best article for soft / hard links practise
Excellent! Explanation, very clear!
Summary: hardlinks are in fact just the principle of pointers/references from program code applied to file system entries on a storage medium.
With pointers or references in code, you can have multiple references to the same variable, the same part of memory with the same data.
More interesting: COW (Copy on write) file systems, like Btrfs or ZFS. They initially follow the principles of hardlinks to save disk space, until you modify one of both file entries. At the moment of modification, an actual copy of the data is automatically made.
Thanks Eduard.
what happens with a Sym link if the source file is deleted then recreated? does the sym link revive, or must the symlink be recreated?
If you recreate the original file, the symlink will work again.