Hard & Symbolic Links
The ln command is used to create links between files. There are two types of links: HardLinks & Symbolic Links
ln [options] {target name} [link name]
| Options | Function |
| -backup | Back up existing destination files. |
| -f | Remove existing destination files. |
| -s | Symbolic links. |
| -i | Prompt to remove destination files. |
| -v | Print the name of a file before linking. |
Hard Links
-
A hard link is an additional name for an existing file.
-
Points directly to the inode (underlying data).
-
All hard links to a file are equal – there's no "original" vs "copy".
-
If the original file is deleted, the data remains as long as one hard link still exists.
- You cannot create hard links between directories.
- You cannot create hard links between different filesystems.
ln sourcefile link_name
Visual representation of a hard link:
[file1] ─┐
│
[inode X]──> "hello"
│
[file2] ─┘
Recognising Hard Links
When a file has a hard link on it, the OS doesn't make it massively obvious. IE when you set a symbolic link, it will be obvious when viewing the file that there's an existing link:
lrwxrwxrwx 1 root root 9 Jun 16 14:21 test3.txt -> test1.txt
But hard links don't show this. Instead, the best way to check if 2 files are hard-linked would be to check their inode numbers using ls -li
Symbolic Links
A symbolic link is a pointer or shortcut to another file or directory. It stores the path to the target, not the data itself.
ln -s target link_name
Example
ln -s /etc/config.cfg ~/myconfig
This creates a symbolic link myconfig in your home directory that points to /etc/config.cfg.
No Comments