Datenträgerverwaltung

Soft (Symbolic) Link vs Hard Link

Soft (Symbolic) Link vs Hard Link
Symbolic links (also called soft links) and Hard Links are a resource to access files or directories from any location. This article will describe the differences between symbolic links and hard links.

Hard Links

Hard links are not a file containing the path to the original file but mirror copies of the original file they point to. A file and it's hard links aren't associated by the name or path but by the inode which stores information on the file, like it's location, creation date, permissions and other attributes. Each inode number is unique within a filesystem preventing hard links from working between different partitions or systems. Hard links can't be used to link directories.

In contrast to soft links, hard links contain the information they link to so if the original file is removed you can still access it's data.

In all Linux systems both hard and symbolic links are created with the command “ln”. To get started first create a file called LinuxHintFile and a directory called LinuxHintDirectory as shown in the image below:

touch LinuxHintFile
rmkdir LinuxHintDirectory
ls
root@LinuxHint:/home/linuxhint#

To create a hard link called Link2LHFile  to the file LinuxHintFile run in the terminal:

ln LinuxHintFile Link2LHFile

After checking with ls we see the hard link we just created, if you type ls -i you'll see the files' inodes, you can confirm the link we just created has the same inode number than the original file, type:

ls -i

As we said above, we can't use hard links for directories nor links to other volumes or filesystems, we'll link LinuxHintDirectory with a soft link.

You can find hard links within the working directory by running:

find -type f -links +1

You can also find files by inodes, for this run:

find . -inum

As you see it found both the original file and the hard link sharing the same inode number.

 

Soft Links

In contrast to hard links, soft links are not copies of the original file, they contain the path to the original file, because of this if the original file is removed the soft link or symbolic link will point to no file becoming a broken link, or an orphaned link, which means if you loss the source file, if you delete or move it the symbolic link will loss access to the information, while with the hard link the information remains despite the source file removal because it is a full and exact copy of that file.

Also in contrast to hard links symbolic links don't share the same inode with the original file, that's why symbolic link can cross volumes and filesystems while hard links can't. Symbolic links can be used to link directories while with hard links that's not possible.

Symbolic links are also created with the program ln, but for the link to be symbolic/soft we need to specify the parameter -s, to create a symbolic link to the directory LinuxHintDirectory called LHDLink run:

ln -s LinuxHintDirectory LHDLink

And then run 'ls -i' to check it was properly created:

As we see in this case the symbolic link was created, but it has a different inode number than the original file.

We can list symbolic links within a directory with the command “ls -l

In the image above we can see in the permissions of the first file the prefix “l” for symbolic links, the end of the line also shows to what file the symbolic link points.

I hope you found this article useful to understand differences between hard links and symbolic links and how to create them. Keep following LinuxHint for more tips and updates on Linux.

Top Oculus App Lab-Spiele
Wenn Sie Besitzer eines Oculus-Headsets sind, müssen Sie über Sideloading informiert sein. Sideloading ist der Prozess der Installation von Inhalten, ...
Top 10 Spiele zum Spielen auf Ubuntu
Die Windows-Plattform war eine der dominierenden Plattformen für Spiele aufgrund des großen Prozentsatzes von Spielen, die heute entwickelt werden, um...
Die 5 besten Arcade-Spiele für Linux
Heutzutage sind Computer ernsthafte Maschinen, die zum Spielen verwendet werden. Wenn du den neuen Highscore nicht bekommst, weißt du was ich meine. I...