Linux: Hardlink vs Softlink(Symbolic link)


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.
why these links are used?
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
Hard Links Example:
Create a hard link:
ln /home/user/file1.txt /home/user/hardlink.txt
Now,
file1.txt
andhardlink.txt
both point to the same inode on the filesystem.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.Delete
file1.txt
:bashCopy coderm /home/user/file1.txt
Even after deleting
file1.txt
, the data is still accessible throughhardlink.txt
because it's just another name for the same file data.
Symbolic Links Example:
Create a symbolic link:
bashCopy codeln -s /home/user/file2.txt /home/user/symlink.txt
Now,
symlink.txt
is a symbolic link pointing tofile2.txt
.Modify
file2.txt
:bashCopy codeecho "Hello from file2.txt" >> /home/user/file2.txt
The modification in
file2.txt
will be reflected insymlink.txt
because it's pointing to the same target file.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
Subscribe to my newsletter
Read articles from Sanjog directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
