The Linux and Unix Links: The Symbolic Links versus The Hard Links

A link is a reference to another file, giving the file multiple names and allowing it to be in more locations at once. There are two kinds of links: symbolic links (also known as symlinks or soft links) and hard links.

By deleting the original file, the symlink will be broken, pointing to an invalid file path.

A hard link is simply a second name for the file on the disk (the hardlink has the same inode as the targeted file, it is also called an alias of the file).

$ stat -c "%i %F %n" myfile
428317 regular file myfile
$ stat -c "%i %F %n" symlink
428328 symbolic link symlink
$ stat -c "%i %F %n" hard_link
428317 regular file hard_link

By deleting the original file, the hard link will still be valid.

Both the symlinks and the hard links are created with the ln commad:

symlink: ln -s /path/to/file /path/to/symlink

hard link: ln /path/to/file /path/to/hard_link/

To view the pointed file in a symlink, use ls -l or readlink:

$ ls -l link
lrwxrwxrwx 1 razvan razvan 6 2012-07-27 12:21 link -> myfile
$ readlink link
myfile

I have found the following usefull figure in the Second Edition of the Linux Pocket Guide by Daniel J. Barrett:

Symbolic Links versus Hard Links:

1.The hard link has the same inode (is a” file alias” of the targeted file) as the targeted, while the soft link is a path name redirect (has a different inode than the targeted file).

2. Hard links must be on the same filesystem / partition since an inode on one disk has no meaning on another, while soft links can cross filesystems.

3. Hard links files stay linked if you move either the targeted file or the link, while the soft links break when you move the targeted file or the link itself, if you used a relative path in the symlink.

4. Because the hard links are copies (aliases) of the original file, they will still be valid if the original file is deleted. If the target of the symlink is deleted, the symlink becames unusable.

5. Softlinks can point to any target, even directories, but most filesystems disallow hard linking directories, with the exception of the Unix . and .. hard links

If you’ve enjoyed this article, read the other Linux and Unix link articles here.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!