Symbolic and Hard Links are useful ways to reference to information on a disk, both found in UNIX and Linux systems. While they seem similar in the surface they are quite different in how they work and it what can be achieved when using them.
If you find the articles in Adminbyaccident.com useful to you, please consider making a donation.
Use this link to get $200 credit at DigitalOcean and support Adminbyaccident.com costs.
Get $100 credit for free at Vultr using this link and support Adminbyaccident.com costs.
Mind Vultr supports FreeBSD on their VPS offer.
Symbolic Links
This type of links is also known as soft links, and the terms can be used interchangeably. This type can be thought as a shortcut in Windows. In other words, soft links act as a second reference in a different location for one file on the system.
Symbolic links can be used to point to any type of file. A symbolic links pointed to an executable, when used, will allow the execution of the file. If the type of file pointed to is a directory, when using cd towards the symbolic links we will be directed to the destination of the link. They can also be pointed to devices, such as removable drives, CD-ROM, etc.
In the case a symbolic link is removed on the link is removed, the destination file where it was pointing to remains without consequence. If the destination file is removed though, the link will be pointing at nothing.
Creation syntax
$ ln -s <path to the file to be linked> <path to the file to be created as a link>
Example:
We’ve created an empty file called file1. Then we have added a link to it called link2file1 with the ‘ln’ command with the ‘-s’ flag. When listing the directory contents we can see the difference and how the symbolic links is pointing to file1.
Let’s now at some content to file1 and then see what happens when we use the ‘cat’ command to display the contents of the symbolic link named link2file1.
As we can see above anything written to fil1 is being referenced when calling the link.
We can now try a different test and remove the destination file1 and see how the links is listed and behaves.
Despite file1 being removed the symbolic link file named link2fil1 is still pointing at it. What will it show when using cat to it?
The links still exists but it’s pointing to a non-existent file so it can’t display anything.
This later case can be useful when using it for external pluggable devices. Once the device is connected to the system once it is mounted the content from it can be used via the symbolic link.
To remove a symbolic link, we will use the unlink command.
Removing syntax
$ unlink <path to the symlink file>
Hard links
Hard links are created with the same command, the ln, but they are a different thing. While in soft links we are creating two files where one is connected to the other via pointing to it. This can be better understood when comparing the size of each of the files.
Note file1 is taking 15 bytes of space while link2file1 is taking just 5 bytes, despite both displaying the same content. This is because a symbolic link is pointing to the reference file. In this case link2file1 is pointing to file1.
However, on hard links the information is stored in one place in the disk, while two files hold the reference to the exact inode location. These files can also be allocated in completely different paths. And when removing one of them the content is still in place and can be accessed via the other one.
Example:
We have created a file named file3.txt with the content “This is a hard link”. Then we have made a hard link with the ln command and named this second file hardlink.
When listing the content, we can see that both file3.txt and hardlink files occupy the same amount of space in disk.
Furthermore, they both display the same content when using cat on them, as it happened with symbolic links. However, let’s see what happens when we are removing the first file which had content on it, the file3.txt.
As it can be seen the file3.txt file doesn’t exist but the hardlink file is still displaying the same content when using the cat command. This is because both where pointing to the same exact location in disk.
To remove hardlinks we will use the same ‘unlink’ command as before.
The ‘stat’ command
It becomes more apparent than ever what a hardlink is when using the ‘stat’ command.
Both files are pointing to the same exact inode and occupy the same amount of disk space.
Conclusion
Symbolic and hard links in UNIX and Linux are useful tools. One allows altering a file in one location to affect the content of a same named file somewhere else in the system. This is used for example when configuring some files in Debian, like in the Apache HTTP server configuration. Hard links help pointing two different files, in different locations, to the same exact inode, effectively making them the same. Removing one of the files will not remove the content, since the inode has a second reference with the other file pointing at it.
If you find the articles in Adminbyaccident.com useful to you, please consider making a donation.
Use this link to get $200 credit at DigitalOcean and support Adminbyaccident.com costs.
Get $100 credit for free at Vultr using this link and support Adminbyaccident.com costs.
Mind Vultr supports FreeBSD on their VPS offer.