Ln Command: How to Create Symbolic Links in Linux

September 24, 2020

Introduction

A link creates a reference to a file or a folder. Symbolic links are used in Linux for managing and collating files.

In this guide, learn how to use the ln command to create symbolic links in Linux.

Ln Command in Linux: How to Create Symbolic Links

Prerequisites

  • A system running Linux
  • Access to a terminal window / command line (Activities > Search type Terminal)
  • (optional) A user account with sudo or root privileges (needed to access certain protected files and directories)

Ln Command to Create Symbolic Links

To use the ln command, open a terminal window and enter the command with the following format:

ln [-sf] [source] [destination]
  • By default, the ln command creates a hard link.
  • Use the -s option to create a soft (symbolic) link.
  • The -f option will force the command to overwrite a file that already exists.
  • Source is the file or directory being linked to.
  • Destination is the location to save the link – if this is left blank, the symlink is stored in the current working directory.

For example, create a symbolic link with:

ln -s test_file.txt link_file.txt

This creates a symbolic link (link_file.txt) that points to the test_file.txt.

To verify whether the symlink has been created, use the ls command:

ls -l link_file.txt
Creating a link using the ln command

Create a Symbolic Link to Linux Directory

A symbolic link can refer to a directory. To create a symbolic link to a directory in Linux:

ln -s /mnt/external_drive/stock_photos ~/stock_photos

This example creates a symbolic link named stock_photos in the home (~/) directory. The link refers to the stock_photos directory on an external_drive.

Creating a link to a directory with the ln command

Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can be linked to resources on those remote systems.

Force Overwrite Symbolic Links

You might receive an error message as displayed in the image below:

example of Error message: link file already exists


The error message means that there’s already a file in the destination named link_file.txt. Use the -f option to force the system to overwrite the destination link:

ln -sf test_file.txt link_file.txt
Overwriting an existing link file

Note: Using the -f option will permanently delete the existing file.

Deleting or Removing Links

If the original file is moved, deleted, or becomes unavailable (such as a server going offline), the link will be unusable. To remove a symbolic link, use either the rm (remove) or unlink command:

rm link_file.txt
unlink link_file.txt
Deleting link files using rm and unlink commands

Soft Links vs Hard Links

The ln command can be used to create two different kinds of links:

  • Soft links
  • Hard links

Soft (Symbolic) Links

A soft link, sometimes called a symbolic link or symlink, points to the location or path of the original file. It works like a hyperlink on the internet.

Here are a few important aspects of a soft link:

  • If the symbolic link file is deleted, the original data remains.
  • If the original file is moved or deleted, the symbolic link won’t work.
  • A soft link can refer to a file on a different file system.
  • Soft links are often used to quickly access a frequently-used file without typing the whole location.

Hard Links

When a file is stored on a hard drive, several things happen:

  • The data is physically written to the disk.
  • A reference file, calledinode, is created to point to the location of the data.
  • A filename is created to refer to the inode data.

A hard link works by creating another filename that refers to the inode data of the original file. In practice, this is similar to creating a copy of the file.

Here are a few important aspects of hard links:

  • If the original file is deleted, the file data can still be accessed through other hard links.
  • If the original file is moved, hard links still work.
  • A hard link can only refer to a file on the same file system.
  • The inode and file data are permanently deleted when the number of hard links is zero.

Conclusion

You should now have a solid understanding of hard and symbolic (soft) links, and how to work with them. Use the ln command to create links and verify using the ls command.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Use the sudo Command in Linux
August 18, 2020

sudo stands for SuperUser DO, and it's used to temporarily elevate privileges in Linux. This guide will show...
Read more
19 Crucial Linux ls Commands to Know
July 30, 2020

The ls command (short for list) lists information about directories and any type of files in the working...
Read more
How to Use Linux Cat Command (With Examples)
July 13, 2020

The cat command, short for concatenate, is used to display the contents of one or more files, without having...
Read more
Linux Commands Cheat Sheet: With Examples
February 21, 2020

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more