« Back to News

Useful “Links” in Linux

A very useful Linux feature – although one that is often misunderstood – is the link. Linking lets you create a file’s shortcut to link one or more files. In simple words, link is basically a pointer to another file. Linux provides two different types of links:

  • Hard link
  • Soft link (or symbolic link).

Introduction to InodesLinks-in-Linux-banner

To better understand how linking works, we need to first understand the concept of inodes. In Linux and other Unix-like operating systems, each file contains an index number called an inode (sometimes referred to as an “index node”). An inode is a data structure on a file system that stores all the information about a file except its name and its actual data.

Linking utilizes inodes by pointing a file towards a specific inode value.

Figure 1.1

What happens when someone tries to access a file?

When a user tries to access any file or any information related to the file, the user uses the file name to do so. However, internally the file name is first mapped with its inode number stored in a directory table. Then through that inode number the corresponding file/data is accessed.

Tip: To view a file’s inode number, issue the command ls –i within the command prompt:

Hard Links:

As explained earlier, every file in linux system has an inode. Every name connected to an inode can be considered a hard link. A hard link essentially creates a mirror copy of the original linked file.

Each hard linked file is assigned the same inode value as the original; therefore, they both reference the same physical file location. It means there is no new inode creation in case of hard link. Any change to the data in either file is reflected in the other.

Hard links are beneficial as they are more flexible & remain linked even if the original file is moved throughout the file system (although hard links are unable to cross-different file systems). Hard links can only be assigned to files and not directories.

Note: Even if you rename the original file (or move the original file), a hardlink still points to the file. If you replace the original file with another file of same name (by copying it), a hardlink will not point to the new file (because the inode is changed).

Figure 1.2

Tip 1:  Command to make hard link : ln [original filename] [hardlink name]

Note: Here the inode for both the files are the same.

Tip 2:  Discover how many hard links exist for a file, as hard links reference the inode directly on the disk.

How? … In the inode information, you have an option for “Links”, which will tell how many links exists to a file. Issue this command: stat file name> and see the result.

A Point to remember: A file will only be deleted from disk when all the hard links to its inode is deleted (you can rm or unlink the hard link).

Soft links (or sym links or symbolic links):

Soft links are very similar to the “shortcuts” feature in windows. It is a way to link a file or a directory.

The most important advantage of soft link is that it can be used to refer to a file that is placed anywhere, even on another computer. The soft link will still work. However, the biggest disadvantage is that the soft link is naturally dependent on the original file. If the original file is removed (or even renamed), the soft link will no longer work (called hanging link or dangling link).

In more technical words, in a soft link, a new file is created with a new inode, which has the pointer to the inode location of the original file.

Figure 1.3

Tip: Command to make soft link: ln –s [original filename] [softlink name]

Note: Here, the inode for both the files are different. You can check the inode value by using “ls  -i” command.

Fix a hanging link?

The broken link will typically be indicated by red colored text. To fix a hanging link follow these instructions:

  1. Find the broken link – using command ls –l
  2. Get the original file location – using command find –name “FileName” 
  3. If the original file exists, then reissue the softlink to that new location of original file. Follow these steps:
    1. Delete the existing softlink – using command unlink [link name]. Remember: When you issue the unlink command, the soft link file will disappear.
    2. Configure a new softlink to the new location.

Tip: If the original file is deleted or not available, remove the hanging link with the ‘unlink [link name]’ command to remove the soft link file.

When to use Soft Link?

  1. Linking across filesystems: A soft link can be created for a file which is placed across filesystem.
  2. Directory linking: If you want to link directories, then you must be using Soft links, as you can’t create a hard link to a directory.

When to use a Hard Link?

  1. Less storage space: No new inodes are created in hard links; therefore, it takes very negligible amount of space. In soft links, we create a file with different inodes, which consumes space (depending upon the file system).
  2. Better performance: A hard link has slightly better performance, as you are directly accessing the disk pointer instead of going through another file.
  3. Changing location: If you move the source file to some other location on the same file system, the hard link will still work, but soft link will fail.
  4. Safety: In hard link, the data is safe, until all the links to the files are deleted, whereas in soft link, you will lose the data if the original file is deleted.

A Quick Recap:

Hard Links:

  • Use the “ln [original filename] [link name]” command to create a hard link.
  • Original file and hard linked file have the same inode value.
  • Any changes to either original file or hard linked file are reflected in the other file.
  • Hard link will remain intact even if the original file is renamed.
  • Pros – Hard links are more flexible and remain linked, even if either the original or hard linked file is moved throughout the same file system.
  • Cons – Hard links are unable to cross different file systems & cannot be linked to a directory.

 Soft Links:

  • Use the “ln -s [original filename] [link name]” command to create a soft link.
  • Similar to shortcut feature in Windows OS.
  • Original file and soft linked file have different inode values.
  • Soft link will be broken if original file is renamed.
  • Any changes to either original file or soft linked file are reflected in the other file.
  • Pros – Soft linked file can cross different file systems & can linked to directory as well.
  • Cons – If original file is deleted or moved, the soft link is broken (hanging link).



References: http://www.geekride.com/


Comments are closed.

About the Writer

  • Upasana Kumari
    Senior Software Developer at Synerzip

How can Synerzip Help You?

By partnering with Synerzip, clients rapidly scale their engineering team, decrease time to market and save at least 50 percent with our Agile development teams in India.