How to Create Symbolic Links Using the ln Command

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

Symbolic links work like shortcuts to make really long paths shorter and to access to 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?

Each file in your file system is identified by an inode. 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 assign a different name to a file in a different location, but it's still exactly the same file. The key that links the files together is the inode number. The great thing about hard links is that they don't take up any extra physical hard drive space.

A hard link facilitates efficient file categorization. For example, in your photo collection, create one folder called vacation pictures, another folder called kids photos, and a third one called pet photos. Instead of copying a vacation photo of your children with Fido and Spot into all three folders, put it into one and create a hard link to it in the other two folders.

Execute the following command to create a hard link:

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

A photo called BrightonBeach in the vacation photos folder links into the kids 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 (i.e. it is not linked). If the number is greater than one then it is hard-linked by 2 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 actual 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 on other devices.

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

Creating 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 a link that already exists 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 already exists by creating the same filename but with a tilde at the end. If a file already 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 already 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 and 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