Hard links and symbolic (soft) links

Introduction

There are two types of file links in Linux: hard links and symbolic links (soft links)

Hard link - this is simply an alternative file name that points to the same data on the hard drive. In practice, all hard links to a single file have the same weight because each of them points to exactly the same blocks of data.

Symbolic link (symbolic link or soft link) - is a separate file that contains the path to the file or directory to which the symbolic link refers. It is more flexible than a hard link, but depends on the existence of the original file.

If the above explanations seem unclear, try the commands below.

Example

Hard links

First, let's create a test file test1.txt, containing the text "abc":

echo abc >  test1.txt

To create a hard link called test2.txt, issue the command:

ln test1.txt   test2.txt

The file was not copied, but only acquired a new name pointing to the same area in memory. Now both names are equivalent. Removing the original name will not result in data loss as there is still a hard link called test2.txt pointing to this data.

So let's remove the original name test1.txt:

rm test1.txt

Using the cat command we will display the data from the test2.txt file:

cat test2.txt

The result, of course, is the text "abc". To find out how many hard links a given file has, you can run the ls -l command on one of its names: 

ls -l test1.txt

Result:

-rw-r--r-- 2 runner21 runner21   4 Jan 14 12:46 test1.txt

The number 2 indicates that there are two hard links. Of course, they can be created in different places. To find all the places where they are located, run the command on one of the names (no matter whether it is test1.txt or test2.txt):

find  /  -samefile  test1.txt

where / is the starting point of the search (root directory). You can customize this path to narrow down your search, e.g. to the /home directory:

find /home -samefile test1.txt

The result will be all paths specifying the same file.

Symbolic links

Let's create a test file test1.txt containing the text "abc":

echo abc >  test1.txt

To create a symbolic link, issue the command:

ln -s test1.txt  test2.txt

From now on, test2.txt points to test1.txt, but the two files are not equivalent. The test2.txt file is only an indication of what is visible when we execute the ls command:

ls -l test2.txt

The result is:

-rw-r--r-- 1 runner1 runner1 4 Jan 14 12:46 test2.txt -> test1.txt

The number specifying hard links has not changed, but we have received an explicit indication of what file the name test2.txt points to.

Warning: deleting the test1.txt file will result in data loss!

So if we delete test1.txt:

rm test1.txt

then test2.txt symbolic link will still exist, but will point to a non-existent file

cat test2.txt

as a result we will receive a message:

cat: test2.txt: No such file or directory

What's more interesting, if we now create a test1.txt file again with different contents but the same location:

echo cde > test1.txt

then test2.txt will now point to it (because the symbolic link has not changed):

cat test2.txt

The result is the text "cde".

If we want to find all symbolic links of the test1.txt file, we must issue the command:

find  /  -type  l  -name test1.txt

where / is the starting point of the search (root directory), and l specifies the search for a symbolic link. You can customize this path to narrow down your search, e.g. to the /home directory:

find /home  -type l  -name test1.txt


Summary

It is worth noting that:

  • Hard links can only point to another file, while symbolic links can point to files or directories
  • Hard links can point to the same file on the same file system, while symbolic links can point to files and directories that are located on different file systems, partitions, or even remote locations!


Good luck!

#buymeacoffee