Image taken from:

Hard links and Symbolic links on UNIX, UNIX-LIKE OS


When working in a UNIX, UNIX-like operating system we can find a file that has many names, this is not a mistake; it is a tool that allows to reference files in other parts of the system easily and quickly.

There are two types of names, or links: hard links and symbolic links, both tools are similar since both serve to reference files in other directories but there are some differences between them, which we will talk about later.

So, what is a hard link?

A hard link is basically another name for an existing file on a machine that uses UNIX or UNIX-like OS, there can be as many names for a file as required, so there can be as many hard links to a file as required, the operating system does not make any difference between a hard link and the original name of a file; for the operating system the hard link is seen as a normal file, so when using a command like ls the hard link will be listed as a file of the same type as the original file.

However, hard links are not allowed in directories and they cannot cross filesystem boundaries or span across partitions.

How do you create a hard link?

A hard link is created with the command ln, for example, if you need to create a hardlink named b for the file a.txt you would use the following command to create the link in the current working directory:

ln a.txt b

Done with hard links, but what about symbolic links?

A symbolic link also known as a soft link, is very similar to a hard link, but with some differences, a soft link can be used to reference directories as well as to files on different filesystems and on different partitions, in addition, when using a GUI, symbolic links have an icon that makes them easily distinguishable from non-linked files.

How do you create a symboliclink?

A symbolic link is created with the command ln and the option -s, for example, if you want to create a symbolic link with the name d to a file named c.txt in the current working directory you can use the following command:

ln -s c.txt d

In a few words, What are these links for?

Perhaps the most useful application for links is to allow files, programs and scripts to be easily accessible in a directory other than the original file or the executable file. Writing the name of the hard link will make the program or script run in the same way as with its original name. Using a soft link instead of a hard link will make the files or directories accessible from any part of the machine software.