How to Create Symbolic Links Using the ln Command

Hard and soft links facilitate effective file and folder structures in Linux

What to Know

  • Create a hard link: ln /path/to/file /path/to/hardlink. Check how many files link to the same inode with the ls command: ls -lt
  • Create a symbolic link: ln -s /path/to/file /path/to/link. Avoid overwriting an existing link: ln -s -b /path/to/file /path/to/link
  • To overwrite the existing link: ln -s -f /path/to/file /path/to/link. To get an overwrite alert: ln -s -i /path/to/file /path/to/link

This article explains how to create symbolic links using the ln command in Linux. Symbolic links work like shortcuts to make long paths shorter and to access files on other partitions and drives.

Getting Started

Linux supports two primary methods of linking:

  • Hard links: Links to an actual file on the Linux system.
  • Soft links: Similar to a Windows shortcut, which points to the original file location. Also known as a symbolic link.

What Is a Hard Link?

An inode identifies each file in your file system. Most of the time, you don't use this number, but the importance of it comes to light when you create a hard link.

A hard link assigns a different name to a file in a different location, but it's still the same file. The key that links the files together is the inode number. The great thing about hard links is that these don't take up extra physical hard drive space.

A hard link facilitates efficient file categorization. For example, in your photo collection, create one folder called holiday photos, and another folder called kids photos. Instead of copying a vacation photo of your children into both folders, put it into one, then create a hard link to it in the other folder.

Execute the following command to create a hard link:

ln /path/to/file /path/to/hardlink

A photo called BrightonBeach in the holiday photos folder links to the kids photos folder through the command:

ln /holidayphotos/BrightonBeach.jpg /kidsphotos/BrightonBeach.jpg

Check how many files link to the same inode by using the ls command as follows:

ls -lt

The output will be something like -rw-r--r-- 1 username groupname date filename.

The first part of the output shows the user's permissions, but the important part is the number after the permissions and before the username.

A screenshot of ls -lt command results

If the number is 1, it is the only file pointing to a particular inode (that is, it isn't linked). If the number is greater than 1, then it is hard-linked by two or more files.

Illustration of a person using a Linux computer
Lifewire / Adrian Mangel

What Is a Symbolic Link?

A symbolic link is like a shortcut from one file to another. The contents of a symbolic link are the address of the file or folder that is being linked to.

The benefit of using symbolic links is that you can link to files and folders on other partitions and other devices.

A difference between a hard link and a symbolic link is that a hard link must be created against a file that exists, whereas a soft link can be created in advance of the file it points to is created.

Create a Symbolic Link

To create a symbolic link, use the following syntax:

ln -s /path/to/file /path/to/link

If you are worried about overwriting an existing link, use the -b switch as follows:

ln -s -b /path/to/file /path/to/link

This switch creates a backup of the link if it exists. It creates the same filename but with a tilde at the end. If a file exists with the same name as the symbolic link, you will receive an error.

Force the link to overwrite the file by using the following command:

ln -s -f /path/to/file /path/to/link

Don't use the -f switch without the -b switch, as you will lose the original file.

Receive a message asking whether you want to overwrite a file if it exists:

ln -s -i /path/to/file /path/to/link

Most modern file-explorer applications in Linux let you create a symbolic link using the GUI. For example, using Thunar File Manager, inside any folder, right-click a blank area of the file list, then select Create symlink.

Screenshot of creating a symlink in Thunar File Manager

This command creates what looks like a new folder. It's actually a symbolic link pointing to the folder where you created it. Paste this symlink anywhere else you like, and when you open it, it will always open the original folder where you created it.

How Do You Tell if a File Is a Symbolic Link?

Run the following ls command:

ls -lt

If a file is a symbolic link, you will see something like this:

myshortcut -> myfile

Was this page helpful?