what are hard and symbolic links on Linux, how to create them, and what is the difference between the two ?

1- Hard and symbolic links on Linux :

      Before explaining and define the two links we should define what is a link on Linux. A link in Linux is a pointer to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file. There are two types of links : Symbolic and Hard links .

Symbolic links :

      A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. A symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. 

How we create a symbolic link :

 ln -s source_file myfile

img_58656ab05449e

Hard links :

A hard link is essentially a label or name assigned to a file. Conventionally, we think of a file as consisting of a set of information that has a single name. However, it is possible to create a number of different names that all refer to the same contents. Commands executed upon any of these different names will then operate upon the same file contents.

How to create a hard link :

 ln source_file myfile

hardlink-56747ee25f9b586a9e497536

Differences between symbolic link and hard link

Let’s summarize our findings. The list bellow summarizes some differences between symbolic and hard link:

  1. Hard link cannot be created for directories (folders). Hard link can only be created for a file.
  2. Symbolic link can link to a directory (folder).
  3. Removing the original file that your hard link points to does not remove the hard link itself; the hard link still provides the content of the underlying file.
  4. If you remove the hard link or the symbolic link itself, the original file will stay intact.
  5. Removing the original file does not remove the attached symbolic link but without the original file, the symbolic link is useless (the same concept like Windows shortcut).

 

Leave a comment