Hard link vs Symbolic link
I will explain what a hard link and a sybolic link are, how to create each one and the differences between these two.
Let’s start defining them, symbolic link or soft link is a link or connection to a file that does not contain the information of this one, an example of this are the known shortcuts in Windows, these do not contain the data of the file to which they point, they are only connections with a real program. If the original program is deleted, so are its symbolic links.
The hard link is defined as a copy or mirror of the parent file which does contain the data of this one, which means that if the file it points to is deleted the hard link will not be deleted and will still have its contents.
Differences between them:
- Soft link: You can create a soft link connection between disks (hard drive — ssd).
- Hard link: You can not create connections outside the same disk (hard drive — ssd).
- Soft link: Can create links with directories.
- Hard link: Can not create links with directories, only files.
- Soft link: Does not contain the contents of the ponter directory.
- Hard link: Has the contents of the pointed file.
How to create them:
To create any of the two links we can use the same command ‘ln’.
let’s start by seeing the command syntax:
vagrant@vagrant-ubuntu-trusty-64:~$ ln [OPTIONS] TARGET LINKNAME
As we see, we can put options as in most commands, with the option ‘-s’ we can create a symbolic link, without options we can create a hard link. the ‘TARGET’ is the file to which we are going to point and ‘LINKNAME’ is the name we are going to give to our link.
Knowing all of that lets create both links:
vagrant@vagrant-ubuntu-trusty-64:~$ ln users userslink
vagrant@vagrant-ubuntu-trusty-64:~$ ll
drwxr-xr-x 9 vagrant vagrant 4096 Sep 16 05:03 ./
drwxr-xr-x 5 root root 4096 Sep 11 03:33 ../
-rw-rw-r-- 2 vagrant vagrant 16 Sep 16 05:03 users
-rw-rw-r-- 2 vagrant vagrant 20 Sep 16 05:03 userslink
In the previous example in our current directory we had a file called users, and we created a hard link called userslink, how can we see the two files have the same permissions and the same weight because they have the same content.
vagrant@vagrant-ubuntu-trusty-64:~$ ln -s users userslink
vagrant@vagrant-ubuntu-trusty-64:~$ ll
drwxr-xr-x 9 vagrant vagrant 4096 Sep 16 05:03 ./
drwxr-xr-x 5 root root 4096 Sep 11 03:33 ../
-rw-rw-r-- 2 vagrant vagrant 16 Sep 16 05:03 users
lrwxrwxrwx 1 vagrant vagrant 4 Sep 16 05:09 userslink -> users
In this other example we can see that we added the option ‘-s’ that means that we create a symbolic link, now when we list the contents of the current directory we can see that the name of the symbolic link has an arrow pointing to the name of the file from which the link was created, we can also see that the permissions of the symbolic link has no connection with the permissions of the file to which they point, and also their weights do not match because they do not contain the same data.