Difference Between Hard Links And Symbolic Links

Introduction:

Abderrahmen Babchia
4 min readSep 15, 2020

--

Most of the people who uses Linux don’t really know the difference between Symbolic Links and Hard Links so in this article we’ll be talking about their utilities and the difference between them.

What are Hard links and Symbolic links and why we use them ?

Links:

Links are often used to “store” multiple copies of the same file in different places but still reference to one file.

Hard links:

The most useful application for hard links is to allow files, programs and scripts (i.e. short programs) to be easily accessed in a different directory from the original file or executable file (i.e., the ready-to-run version of a program). Typing the name of the hard link will cause the program or script to be executed in the same way as using its original name.

Symbolic links:

Symbolic links are like shortcuts or references to the actual file or directory. Most of the time these links are transparent when working with them through other programs. For example you could have a link to a directory to one hard drive inside a directory that lies on a different hard drive and an application will still treat it the same.

Symbolic links are used all the time to link libraries and make sure files are in consistent places without moving or copying the original.

Creating a Symbolic Link:

To create a symbolic link in Linux we use this syntax:

ln -s /path/to/original/ /path/to/linkName

What happens if I edit the link? Any modifications to the linked file will be changed on the original file. What happens if I delete the link? If you delete the link the original file is unchanged. It will still exist. What happens if I delete the original file but not the link? The link will remain but will point to a file that does not exist. This is called an orphaned or dangling link.

Creating a Hard Link:

You can create a hard link like so:

ln /path/to/original.file /path/to/link.file

The Difference Between Soft and Hard Links:

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.

Command ln:

ln is a command-line utility for creating links between files. By default, the ln command creates hard links. To create a symbolic link, use the -s ( — symbolic ) option.

--

--