Skip to main content

Basic File Commands

ln Command Examples

Links are one of the essential part of the Linux filesystem. Learn how to create links using ln command in this tutorial.

A link is a reference to another file. Links give the same file multiple names and allowing them to live in two or more locations simultaneously.

There are two types of links:

This picture shows how the two types of links work:

soft link and hard link in Linux

Both hard links and soft links are created with the ln command.

In this tutorial, I'll show you how to use the ln command for creating various types of links in Linux.

Examples of the ln command

The syntax for ln command is simple:

 ln [option] target_file link_name

Let me show you some examples of using the ln command to create links in Linux.

To create a hard link to a file, you can use the ln command without any options like this:

 ln target_file link_name
Creating hard link in Linux with ln command

To create a symbolic link to a file, use the option -s with the target file name and the link name

 ln -s target_file link_name
Creating soft link in Linux using ln command

Most Linux terminals will show the soft link in a different color along with the destination it points to.

You'll also notice that links start with l instead of the usual - for files in the long listing view.

Even if your terminal doesn't show soft links in different color, you can identify links in this way.

Creating a soft link to a directory is the same as creating symbolic link to a file. You just need to replace the target file name/path with the directory name/path.

 ln -s target_directory link_name
Creating soft link to a directory in Linux

You’ll notice that the color of the soft link and hard link is usually different in the Linux terminal. Hard link to a directory is not possible (normally).

You can overwrite an existing link. By default, if you try to use an existing link to point to a new file, it will throw you an error:

ln: failed to create symbolic link 'soft-link-to-file': File exists

The ln command has two options for this purpose:

  • -i: The interactive mode asks you if you want to overwrite the existing link.
  • -f: The force mode just updates the existing link without any confirmation.

Suppose, you want to force update a symbolic link. Here's what you can do:

ln -sf new_file existing_soft_link

Mind to add the s for soft link otherwise you'll convert the soft link to hard link.

Update soft links in Linux

Normally, when you use the ls command with the -loption, it shows the file it points to.

But if there is chain of links, it won't show the original file. For example, you create a link to a file and then create another link to the first link. In the long listing, the second link will point to first link.

To find the original file from a chain of links, you can use the readlink -f in the following fashion:

readlink -f soft_link

The -f options stands for 'follow' as in 'follow the chain'.

This image explains the example better:

Follow chain of links with readlink command in Linux

Now that you know how to create links, let's briefly why do we need links? What practical purpose do they serve?

There could be several use cases. Let's say you downloaded a software that comes with its code and an executable file. You keep the entire thing in the /opt directory. But to run the program from anywhere, you need to put its executable in the /usr/bin directory.

If you move the executable to this directory, it may not work as it needs to access some of the code and it won't find the path to these files from the /usr/bin directory. This is where you can create a link to this executable file in the /usr/bin directory.

This way, the program can be run from anywhere on the system and the executable of the program remains in its original program folder.

Links are an essential part of Linux. You'll find them used at many places in your systems. Just look in the /lib directory and you'll see plenty of soft links.

What next?

I highly recommend reading the following articles that relate to links concept in Linux:

I do hope you learned to use the ln command effectively in this tutorial. Questions and suggestions are always welcome.

Abhishek Prakash