H_e_d
Linuxlinks
Published in
3 min readFeb 7, 2021

--

Links on Linux

By now, most people have heard about Linux. We can thank Linux for most of the websites and software.

This Unix like operating system is famous among programmers for its safety, simplicity and efficiency. A good feature is the Linux Terminal which allows you to create links.
A link is a shortcut to access a file by pointing it. When you create a link you create a pointer : an item containing a data address.
What links are use for ? Mostly for avoiding the need of duplicating data but also to simplify long path around directories and others that make the user’s life easier.

Two types of links

To understand what the links are you must know about inode. The inode (index node) is a data structure referencing to a file or directory. Each one has an inode (number) assigned during its creation. So any file is linking its inode.

  • Hard link:

Making a hard link is creating a file containing the same inode as the file its referring to. Knowing that every file has an entry point directory that point to its inode number, every file has a hard link by default.
You can create one using the ln command:

ln file link 

By doing this you add a file name to the inode of the linked file.

  • Soft “Symbolic” link :

The symbolic link is its own file that point to the linked file or directory by its name.
The link works in the same manner as a shortcut containing the path of the linked file or directory.

Soft links can reach file across the filesystem due to the fact that they are pointing directly to a link of the file and not to the file’s original inode.

You can create a soft link using the appropriate option to the ln command:

ln -s file link 

The -s option is to specify to make a symbolic link instead of a hard link. You first refer to the name of the file and the name of the link you wish to attribute.

After creating the link, we can see using the ls -l command that the first letter shows that the file is a link, and the arrows at the end shows the file which the link refers to.

lrwxrwxrwx 1 hedy hedy 4 févr. 7 16:39 link -> file
  • Hard link vs soft link:

The main difference is the deletion of the linked file. When you delete a hard linked file, the hard link will not be affected and still refers to the original content. If you want to get rid of the file you must delete every hard link to it.
Renaming the file won’t affect the hard link behavior.

As for Soft link, removing the original file will make the link obsolete and point into “nothing”. However to link files across the filesystem only soft link can help you do it.

  • Using links example:
ln -s /dir1/dir2/dir3/test dir4/test

A symbolic link is created in the dir4 linking the test file in dir3.

ls -l dir4/test 
lrwxrwxrwx 1 hedy hedy 20 févr. 7 18:29 dir4/test -> /dir1/dir2/dir3/test

The link is created into the dir4 folder.

--

--