Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. ln

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

OptionDescription
ln target link_nameCreates a hard link. Only works for files within the same filesystem.
ln -s target link_nameCreates a symbolic link. Works with directories and files across different filesystems.
ln -f target link_nameOverwrites the link name if it already exists.
ln -n target link_nameIf the link name is a symbolic link, replaces it without following it.
ln -v target link_namePrints the name of each linked file (verbose mode).

Sample Code

The following examples use this directory structure.

📁 ~/project/ 📁 config/ 📄 settings.conf 📄 original.txt

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 .