Hard Link & Soft Link

Hard Link & Soft Link

ยท

5 min read

Introduction

  • In Linux, a link is a shortcut that allows multiple names to refer to the same file or folder, making it easier to organize and access your data.

  • Links are handy because they help you manage your files better, save space on your computer, and create connections between different files.

Here's why links are useful :

  • Better File Management : Links let you organize your files by making multiple references to the same data. This means you don't have to make copies of files, which saves space and keeps things well arranged.

  • Saving Space : Instead of copying files, you can use links, which saves disk space. This is especially helpful for large files or when you need the same file in different places.

  • Symbolic Relationships : Links allow you to create symbolic relationships between files or folders. This makes it easier to create shortcuts or aliases for files, making them easier to find and use in your file system.

  • Hard Link

  • Soft Link

Inode Number

  • In Unix-like operating systems like Linux, there's something called an "inode" (index node). It's like a file's or directory's ID card.

  • Each file or directory has its own inode, and it holds important info about them.

  • Here's what's inside an inode :

    • Metadata : This is like a file's or directory's details. It includes :

      • Type of the file (like a regular file, a directory, or a symbolic link).

      • Permissions, which control who can read, write, or execute the file.

      • Info about who owns the file and what group it belongs to.

      • Timestamps that tell you when the file was last read, modified, or changed.

      • The size of the file, measured in bytes.

  • Pointers to Data Blocks :

    • For regular files, the inode has pointers to blocks on the disk where the file's data is stored.

    • It might have direct pointers for small files or indirect pointers (like single, double, or triple) for larger files that can't fit entirely in the inode.

  • File System Pointer :

    • Besides data block pointers, the inode usually has a pointer to the file system's data structure, such as the superblock or the inode table.

    • This helps the file system find and manage the inode efficiently.

  • Unique Identifier (Inode Number) :

    • Each inode in the file system has a unique ID called an inode number.

    • This number is like an index or ID for the file or directory and stays the same throughout its life.

    • Inode numbers are assigned in order when files or directories are created.

    • They're used by the file system to quickly find and access the right inode and its data blocks.

  • When a hard link is created for a file, it adds another filename that directly points to the same file's inode.

  • All hard links to the same file share the same data blocks on the disk.

  • Unlike symbolic links (soft links) that point to the target file's path, hard links directly reference the inode.

  • This makes hard links identical to the original file in terms of data content.

  • Since all hard links point to the same inode and share data blocks, any changes made to the file via one hard link will affect all other hard links to the same file.

  • For example, renaming a file through one hard link will rename it for all other hard links.

  • If you change permissions, ownership, or timestamps of a file using one hard link, those changes will apply to all other hard links to the same file.

  • All the hard links to a file act as one. So, no matter which link you use to look at or change the file, it always looks and behaves the same.

    • Syntax : ln <source_file> <hard_link_name>

  • In the above example, if we updated the data in "filename_2.txt" file. It behaves same as "filename_1.txt" and act as one.

  • Both files shares the same Inode number.

    • Syntax : ln -i <source_file> <hard_link_name>

  • Unlike hard links that can only point to files on the same filesystem, soft links (symbolic links) can point to files and directories located on different filesystems.

  • This makes symbolic links handy for creating references across different storage locations.

  • When you create a symbolic link, it keeps the permissions of the target file or directory.

  • However, the symbolic link itself has its own permissions, deciding who can read, write, or execute it.

  • Users accessing symbolic links need appropriate permissions to access the target file or directory.

  • This is determined by both the permissions of the target and the permissions of the link itself.

  • In listings generated by commands like "ls -l," symbolic links are identified by a special indicator (usually an "l" character) in the file permissions field.

  • This indicator distinguishes symbolic links from regular files and directories, making it clear that it's a link rather than the actual file or directory.

    • Syntax : ln -s <target> <link_name>

  • In the above example, it will create a shortcut.txt in Desktop location.

  • we can also check that both files are linked by soft links by using the following command.

    • Syntax : ls -l <path of soft link>

  • In this case, I created the soft link in Desktop location so that I can easily access the file.

Conclusion

  1. Hard links directly connect to the original file's data on the disk, while soft links contain the path to the original file.

  2. Hard links share the same inode number and file details as the original file, whereas soft links have their own set of file details and permissions.

  3. Changes made to the original file are reflected in all hard links, but soft links remain unaffected by changes to the original file.

  4. Hard links are limited to working within the same filesystem, whereas soft links can point to files or directories across different filesystems.

  5. Even if the original file is deleted, hard links still access the data, while soft links become broken or inactive if the original file is deleted.

Did you find this article valuable?

Support Chandra Prakash Reddy by becoming a sponsor. Any amount is appreciated!

ย