Linux Command - ln
ln
- create hard link or symbolic(soft) link to files and directories.
Syntax
1 | ln [OPTIONS] TARGET LINK |
Create a link to TARGET. For hard links, TARGET must exist in file system. Hard link is not allowed for directory. Symbolic(soft) links can hold arbitrary text. Symbolic(soft) link is like a shortcut in Windows.
If you rename/delete the TARGET file, the hard link will continue to point to the file in file system. However, the symbolic link will be useless.
Hard links are only valid within the same file system. Symbolic links can span file systems.
Options
Common Option | Description |
---|---|
-s, --symbolic | make symbolic links instead of hard links |
-v, --verbose | verbose mode |
Examples
Create a hard link to a file
1 | ln file1 link1 |
file1 and link1 has the same inode number.
Create a symbolic link to a file
1 | ln -s file1 link2 |
if you execute ls -li
command, the output looks like this. notice the symbolic link has a different inode number than the target file. it also has an arrow to show its target.
1 | 13901482 -rw-r--r-- 2 xing xing 11 Jan 4 01:54 file1 |
Create a symbolic link to a directory
1 | ln -s ~/Documents/ ~/Desktop/Docs |
Create a symbolic link call Docs in ~/Desktop. The destination is /home/username/Documents/
Force existing link to point to a new target
force a existing symbolic(soft) link to point to a new target, dir in this case
1 | ln -sf dir link2 |