Understanding Symbolic Links and Hard Links in Linux

Photo by Mike Alonzo on Unsplash

Understanding Symbolic Links and Hard Links in Linux

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two can help you effectively manage your filesystem.

Before starting to work with hard and soft links, let's first understand what a link is in Linux.

Every storage device contains files and directories in a collection of blocks.

Information about a file or directory is stored in a data structure called inode. Inodes contain metadata such as who is the owner of the file, when it was created and when it was last accessed, its size, permissions, ownership, and pointers to the data blocks where the file's contents are actually stored etc.

Every storage medium, such as hard disks, flash drives, etc., has its own inodes. These inodes are specific to the filesystem, storage device, and partition. So if we move one text file from the local storage device to the flash drive, it will get a different inode.

Every inode has an inode number that is unique and used by the filesystem to locate the inode.

A directory entry is essentially a table maintained by the filesystem that contains a name for a file or directory and a pointer (reference, often an address) to the inode number.

FilenameInode number
report.txt43132770
image.png43132771
💡
We can not directly view the raw directory entry because the filesystem manages it at a low level. However, we can view information about the files in a directory, including inode numbers, which are part of the directory entries using the commands ls -li or stat

A link is simply an additional directory entry for a file or directory. It is between the filename and the actual data stored on the filesystem. By creating links we can have multiple directory entries (names) for the same file or directory, allowing two or more names for the same thing.

A hard link is a directory entry that points directly to the inode of a file. It acts as a separate file and refers to the exact spot on a hard drive. It is a mirror copy of the original file. Let's look at how to create hard links and find out how they work. To create the hard link, use ln command.

Creating a hard link:

  1. Create an original file:

     echo "Hello World!" > original.txt
    

  2. Create a hard link:

     ln original.txt hardlink.txt
    

  3. Verify the inode numbers of the original and hard link files:

     ls -i original.txt hardlink.txt
    

As we can see, both original.txt and hardlink.txt share the same inode number, indicating they point to the same file content.

Editing the hard link

  1. Make changes to the hard link hardlink.txt:

     echo "Nice to see you World" >> hardlink.txt
    
  2. Check the content of the original file original.txt

     cat original.txt
    

As we can see, changes made to hardlink.txt are reflected in original.txt because they refer to the same inode.

Deleting the original file

  1. Delete the original file:

     rm original.txt
    
  2. Check the content of the hard link hardlink.txt:

     cat hardlink.txt
    

As we can see, the file content is still accessible through hardlink.txt since the inode and data blocks are preserved until all hard links are removed.

Creating multiple hard links

  1. Create another hard link:

     ln hardlink.txt another-hardlink.txt
    
  2. Verify Inode numbers:

     ls -li hardlink.txt another-hardlink.txt
    

Again, both hardlink.txt and another-hardlink.txt share the same inode number, indicating they point to the same file content.

Hard links have the following advantages:

  1. Efficient Storage: Hard links share the same inode and data blocks, which means no additional disk space is used for multiple hard links to the same file.

  2. File Availability: As long as at least one hard link exists, the file's data remains accessible. Deleting one hard link does not delete the file.

  3. Performance: Hard links are typically faster than symbolic links because they point directly to the inode without the need for path resolution.

  4. Transparency: Hard links appear and behave like regular files. There is no distinction between the original file and its hard links.

  5. Consistency: All hard links to a file are always up-to-date. Any changes made to the file through any hard link are immediately reflected in all other hard links.

While useful, there are some limitations to what hard links can do.

  1. Restrictions on Directories: Hard links can only be created for regular files (not directories or special files).

  2. Single Filesystem Limitation: Hard links cannot span multiple filesystems. They only work when the new hard link exists on the same filesystem as the original.

A soft link (symbolic link) is a separate file that contains the path to another file or directory. Unlike hard links, soft links do not reference the inode of the target file directly but instead have their own inodes and contain the path to the target file or directory.

  1. Create an original file:

     echo "Hello World" > original.txt
    
  2. Create a soft link using the command ln -s:

     ln -s original.txt softlink.txt
    
    💡
    An absolute path should be specified when creating symbolic links since relative paths will not work.
  3. Verify the soft link by typing:

     ls -l original.txt softlink.txt
    

  4. Read the content of the softlink.txt file:

     cat softlink.txt
    

  1. Edit the soft link softlink.txt:

     echo "Nice to see you World!" >> softlink.txt
    
  2. Check the content of the original.txt file:

     cat original.txt
    

As we can notice, changes made through softlink.txt are reflected in original.txt.

Moving the target file

  1. Move (or delete) the original.txt file to another location in the system:

     mv original.txt ./backup/
    
  2. Again, check the content of the softlink.txt file:

     cat softlink.txt
    

As we can see, the soft link becomes a "dangling" link, as its target is moved to another location (or no longer exists in case it is deleted).

Linking to Directories

Because the soft link connection is a logical connection, not a duplication, soft links can point to entire directories or links to files on remote computers. Hard links cannot do this. Let's find out how it works.

  1. Create a folder and a file:

     mkdir my-folder
     echo "Learn how to create soft links to folders" > my-folder/file
    
  2. Create a soft link named soft-folder to the directory my-folder:

    ln -s my-folder soft-folder
    
  3. Verify the link:

    ls -l
    
  4. Access the directory my-folder using soft link soft-folder:

    ls soft-folder
    
  5. Delete the original folder my-folder and list the soft-folder:

    rm -r my-folder
    ls soft-folder
    

As we can see, since the original folder was removed, listing soft-folder shows nothing in the output. The target was deleted and the link became useless.

Soft links have the following advantages:

  1. Flexibility: Soft links can link to both files and directories and span across different filesystems or partitions.

  2. Convenience: It is easy to create shortcuts and organize files with soft links. They can link to non-existent files, which will work once the target is created.

  3. Low Overhead: The size of soft link files is small since they only store the path to the target.

While useful, there are some drawbacks of soft links.

  1. Fragility: They can become "dangling" links if the target is moved, renamed, or deleted.

  2. Performance: They have slightly slower access than hard links because they require an extra path resolution step.

  3. Security: They can potentially introduce security risks if not managed properly, as they can point to sensitive files.

  4. Different Inodes: Soft links have their own inodes, which can add overhead to inode usage.

References

  1. Linux Crash Course - Symbolic Links

  2. Linux Hard Links versus Soft Links Explained

  3. Create and change hard and symbolic links

  4. Hard links and soft links in Linux explained