Linux: Hardlink vs Softlink(Symbolic link)

SanjogSanjog
2 min read

Hard link: Directly links to file data on disk. Deleting any hard link doesn't affect data until all links are removed.

Symbolic link: Simply points to another file or directory. Deleting a symbolic link doesn't remove the target file, but deleting the target file makes the link invalid.

Hard links are used for efficient file organization, backup strategies, and sharing files without duplication.

Symbolic links are used for flexible file referencing, creating shortcuts or aliases, and cross-filesystem referencing.

let's consider an example directory structure:

home/user/

|- file1.txt

|- file2.txt

  1. Create a hard link:

     ln /home/user/file1.txt /home/user/hardlink.txt
    

    Now, file1.txt and hardlink.txt both point to the same inode on the filesystem.

  2. Modify hardlink.txt:

     bashCopy codeecho "Hello from hardlink.txt" >> /home/user/hardlink.txt
    

    This modification affects file1.txt as well because they both point to the same data on disk.

  3. Delete file1.txt:

     bashCopy coderm /home/user/file1.txt
    

    Even after deleting file1.txt, the data is still accessible through hardlink.txt because it's just another name for the same file data.

  1. Create a symbolic link:

     bashCopy codeln -s /home/user/file2.txt /home/user/symlink.txt
    

    Now, symlink.txt is a symbolic link pointing to file2.txt.

  2. Modify file2.txt:

     bashCopy codeecho "Hello from file2.txt" >> /home/user/file2.txt
    

    The modification in file2.txt will be reflected in symlink.txt because it's pointing to the same target file.

  3. Delete file2.txt:

     bashCopy coderm /home/user/file2.txt
    

    After deleting file2.txt, symlink.txt still exists, but it's now a broken link because its target file no longer exists.

    #HardLink #SoftLink #SymbolicLink #Filesystem #Unix #Linux #FileOrganization #BackupStrategies #FileReferencing #FilesystemManagement

0
Subscribe to my newsletter

Read articles from Sanjog directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sanjog
Sanjog