ln
| Since: | All Linux | |
|---|---|---|
| macOS(2001 Cheetah) | ||
| Bash 1.0(1989) |
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
Creating a hard link increases the link count. Even if you delete the original file, the data remains accessible as long as at least one link still exists.
ls -la original.txt -rw-r--r-- 1 user staff 42 Mar 5 10:00 original.txt ln original.txt hardlink.txt ls -la original.txt hardlink.txt -rw-r--r-- 2 user staff 42 Mar 5 10:00 hardlink.txt -rw-r--r-- 2 user staff 42 Mar 5 10:00 original.txt rm original.txt cat hardlink.txt (original file contents are displayed)
Use -L to check whether a symbolic link's target exists. This is useful for detecting dangling symlinks.
ln -s missing_file.txt broken.txt ls -l broken.txt lrwxrwxrwx 1 user staff 16 Mar 5 10:10 broken.txt -> missing_file.txt [ -L broken.txt ] && echo "link exists" || echo "no link" link exists [ -e broken.txt ] && echo "target exists" || echo "target missing (dangling symlink)" target missing (dangling symlink)
Create a symbolic link using a relative path. When the link target is specified as a relative path, the relationship is preserved even if the entire directory is moved.
cd config ln -s ../original.txt local_link.txt ls -l local_link.txt lrwxrwxrwx 1 user staff 15 Mar 5 10:15 local_link.txt -> ../original.txt
Use -v to print information about each created link. Handy for verification and debugging in scripts.
ln -sv original.txt verbose_link.txt 'verbose_link.txt' -> 'original.txt'
Common Mistakes
Common Mistake 1: Using an absolute path in a symlink breaks it after moving the directory
A symbolic link with an absolute path points to a fixed location. If the directory tree is moved (e.g., to another machine or mount point), the link breaks.
ln -s /home/user/project/config.conf /etc/myapp/config.conf (after moving to another server, /home/user/project/ may not exist)
Use a relative path so the link stays valid when the directory tree is moved.
cd /etc/myapp ln -s ../../home/user/project/config.conf config.conf
Common Mistake 2: Forgetting -s creates a hard link instead of a symlink
Without -s, ln creates a hard link. A hard link shares the same inode as the original, so changes in one are reflected in both. Deleting one does not affect the other, which can cause confusion.
ln original.txt hardlink.txt (this is a hard link, not a symbolic link)
Use -s to create a symbolic link.
ln -s original.txt symlink.txt
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.