Admin... by accident!

You may have chosen to be an admin. I didn't!

  • Home
  • FreeBSD
  • GNU/Linux
  • Security
  • Network
  • Virtualization
  • Politics
  • Github
  • Donate
  • Me

Symbolic and Hard Links in UNIX and Linux

July 16, 2023 by Albert Valbuena

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.

 

 

Filed Under: FreeBSD, GNU/Linux, How To's, How To's

Recent Posts

  • How to install Redis for WordPress on FreeBSD
  • How to compile cloudflared in FreeBSD 13/14
  • How to configure FreeBSD to use a webcam (version 12 and 13)
  • Symbolic and Hard Links in UNIX and Linux
  • How to import iocage jails to Bastille on FreeBSD 13
  • How to load and unload kernel modules in Linux
  • How to use find in GNU/Linux and FreeBSD
  • How to install Mate on FreeBSD 12/13
  • How to install Nessus 10 on FreeBSD 12
  • How to enable TLS traffic from the origin server on Cloudflare Argo Tunnel
  • How to use Cloudflare’s Argo Tunnel service to publish a website on FreeBSD 12/13
  • How to setup MariaDB master-slave replication on FreeBSD
  • How to upload a FreeBSD custom image on DigitalOcean
  • How to install Drupal 9 on FreeBSD 13.0
  • How to manage site visitors based on IP Geolocation
  • How to enable Geolocation in AWStats on FreeBSD 13.0
  • How to install AWStats on FreeBSD 13.0
  • How to configure Modsecurity 3 for WordPress on FreeBSD
  • How to configure Apache HTTP with a TLS reverse proxy backend on FreeBSD
  • How to detect a WAF – Web Application Firewall

Archives

  • November 2024
  • October 2024
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • April 2022
  • March 2022
  • October 2021
  • September 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • September 2018
  • June 2018
  • May 2018
  • April 2018
  • February 2018
  • January 2018
  • November 2017
  • April 2017

RSS Admin… by accident!

  • How to install Redis for WordPress on FreeBSD
  • How to compile cloudflared in FreeBSD 13/14
  • How to configure FreeBSD to use a webcam (version 12 and 13)
  • Symbolic and Hard Links in UNIX and Linux
  • How to import iocage jails to Bastille on FreeBSD 13
  • How to load and unload kernel modules in Linux
  • How to use find in GNU/Linux and FreeBSD
  • How to install Mate on FreeBSD 12/13
  • How to install Nessus 10 on FreeBSD 12
  • How to enable TLS traffic from the origin server on Cloudflare Argo Tunnel

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in