ln
The ln command creates links to files. There are two types: a hard link, which gives a file an additional name, and a symbolic link (soft link), which creates a reference to another path.
Syntax
ln [options] target link_name
Options
| Option | Description |
|---|---|
| ln target link_name | Creates a hard link. Only works for files within the same filesystem. |
| ln -s target link_name | Creates a symbolic link. Works with directories and files across different filesystems. |
| ln -f target link_name | Overwrites the link name if it already exists. |
| ln -n target link_name | If the link name is a symbolic link, replaces it without following it. |
| ln -v target link_name | Prints the name of each linked file (verbose mode). |
Sample Code
The following examples use this directory structure.
Create a symbolic link to a file.
ln -s original.txt link.txt ls -l link.txt lrwxrwxrwx 1 user staff 12 Mar 5 10:00 link.txt -> original.txt
Create a symbolic link to a directory.
ln -s /var/www/html /home/user/www ls -l /home/user/www lrwxrwxrwx 1 user staff 13 Mar 5 10:00 /home/user/www -> /var/www/html
Create a symbolic link in a directory on your PATH so you can run the command from anywhere.
ln -s /usr/local/bin/node /usr/bin/node
Create a hard link. Both file names share the same inode number, so they both point to the same data.
ln original.txt hardlink.txt ls -i original.txt hardlink.txt 123456 hardlink.txt 123456 original.txt
Forcibly overwrite an existing link.
ln -sf config/settings.conf link.txt ls -l link.txt lrwxrwxrwx 1 user staff 21 Mar 5 10:05 link.txt -> config/settings.conf
Notes
If the target of a symbolic link is deleted, the link becomes a broken link (dangling symlink). Hard links, on the other hand, keep the data accessible even after the original file is deleted — the data is only removed once the reference count drops to zero.
Symbolic links can point to directories, but hard links to directories are not allowed for regular users. To change file permissions, see chmod / chown / chgrp.
If you find any errors or copyright issues, please contact us.