
- 2025-03-18
- posted by aung.nyithit@spiceworks.co.jp
- System
Soft Link and Hard Link in Linux
What is soft link?
Soft link is similar to window folder or file shortcut feature. Also known as symbolic links or symlinks. Soft links are similar to shortcuts in windows. When the target original file or folder is deleted or moved, the soft link becomes a “dangling” link that points to a non-existing file.
Soft link use case
- to create shortcuts to access files or directories in different locations
- to share resources across file systems
To create soft link to file
ln -s /path/to/original/file.txt /path/to/link/file-link.txt
To create soft link to folder
ln -s /path/to/original/directory /path/to/link/directory-link
To check the link
ls -l /path/to/link #output lrwxrwxrwx 1 aung aung 13 Dec 30 14:59 link_soft_file.txt -> soft_file.txt
What is Hard link?
Hard links can only point to files and cannot link to a file on different partition with a different inode number. Hard links will work when delete the target original file and the data remains accessible by any of its hard links.
Hard link use case
- to create backup copies of important files.
- to avoid duplicating of same data in the same filesystem
- to keep file content accessible even if the original file is deleted
To create hard link to file
ln /path/to/original/file.txt /path/to/link/file-link.txt
To check the link
ls -i /path/to/original/file.txt /path/to/link/file-link.txt #output 6744 hard_file.txt 6744 link_hard_file.txt
- Use soft links for flexibility when using files/directories across filesystems or when needing to link to directories.
- Use hard links for resilience and efficient storage when working with files within the same filesystem.