- Links are pointers to files in a different location
- Compare to shortcuts on other operating systems
- Links can be useful to make the same file available on multiple locations
- Linux uses hard links and symbolic links(don’t call it soft links)
- Create hard links with
In
and symbolic links withIn -s
- symbolic links points to hard links.
- hardlinks points to inode
- inode points to blocks
- if orginal hard link got removed symbolic links becomes invalid
if you want to see the inode
ls -il
- look at the file size and permissions of the symbolic link
- it has size of the string count of the file name
- it has all the permissions but the actual permissions are on the hardlink
1. Hard Links
- Definition: A hard link is an additional name for a file that points directly to the same inode on the disk.
- Key Features:
- Shares the same inode number as the original file.
- If the original file is deleted, the hard link still exists and retains access to the data.
- Works only on the same file system.
- Cannot link to directories (to prevent circular references).
- Use Cases:
- To create backup references of files without duplicating data.
Example:
ln original_file hard_link
2. Symbolic Links (Soft Links)
- Definition: A symbolic link is a special type of file that contains a path to another file or directory.
- Key Features:
- Does not share the same inode as the target file.
- Can span across different file systems.
- Can link to directories.
- If the target file is deleted, the symbolic link becomes a “dangling link” and does not work.
- Use Cases:
- To create shortcuts or references to files or directories, especially when working across file systems.
Example:
ln -s target_file symbolic_link
Differences Between Hard and Symbolic Links
Feature | Hard Link | Symbolic Link |
---|---|---|
Inode Sharing | Shares the same inode as the file. | Has a separate inode. |
File Systems | Works only within the same file system. | Can link across file systems. |
Directories | Cannot link to directories. | Can link to directories. |
After Deletion | File content remains accessible. | Link breaks and becomes unusable. |
Performance | Faster (direct access to inode). | Slightly slower (requires path resolution). |
Common Commands for Practice
-
Create Hard Link:
ln file hardlink
-
Create Symbolic Link:
ln -s file symlink
-
List Links and Check Inodes:
ls -li
-
Remove Links:
- Hard links:
rm hardlink
- Symbolic links:
rm symlink
- Hard links: