How to use the linux ln command

The Linux ln command creates links between files, letting one name point to another file or folder. Links are handy for sharing a single file across locations or giving a long path a short, convenient name. In this article, we will show you the basic usage of the Linux ln command with examples.

Hard links vs. symbolic links

There are two kinds of links. A hard link is a second name for the exact same data, so both names are equal and the data survives until every link is removed. A symbolic link (symlink) is a small pointer to another path, much like a shortcut; if the original is deleted, the symlink breaks. Symbolic links are the more common choice, because they can span file systems and point to directories.

Create a symbolic link

First, use the -s flag to create a symbolic link. Name the target first, then the link:

ln -s /path/to/target linkname

For example, to point public_html/latest at a versioned folder:

ln -s /home/user/releases/v2 /home/user/public_html/latest

Create a hard link

Next, omit -s to create a hard link. Both names then refer to the same file data:

ln target.txt hardlink.txt

Update or overwrite a link

Finally, to repoint an existing link, add -f (force) so ln replaces it, and -n so it does not follow an existing symlinked directory:

ln -sfn /path/to/new/target linkname

You can check where a symlink points with ls -l, which shows the link followed by an arrow to its target. A common use of the Linux ln command in web hosting is pointing a folder such as public_html/current to the latest release, so you can deploy a new version and switch the link in a single step. If a link ever breaks, simply recreate it with ln -sfn and the new path.

Need more help?

Explore more Linux commands with examples on an SSH shared hosting account, or get full root control with a JetHost VPS.