Using ln command in Ubuntu
Commands

Use ln Command to Create Links in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

The ln command is used to create symbolic links in Linux.

And in case, you don't know, there are two types of symbolic links: soft links and hard links.

The key difference between a soft link and a hard link is where it is pointing to.

The soft link points to the filename, whereas the hard link points to the data block.

And in this guide, I will show you how you can create both using the ln command.

Using the ln command in Ubuntu

To learn any command, it is necessary to learn the syntax first.

So to use the ln command, you'd have to use follow the given syntax:

ln [options] [source-file] [Link-name]

Here,

  • [options]: are used to tweak the default behavior of the ln command
  • [source-file]: This is where you'd enter the name or the absolute path to the file of which you want to create the symlink.
  • [Link-name]: is where you'd enter the name of the symlink or you can also provide the absolute path here if you want to save it somewhere else.

Now, let's have a look at some examples of how you can use the ln command.

To create a soft link, you'd have to use the -s flag with the ln command as shown:

ln -s [target-file] [link-name]

So let's say I want to create a soft link named MintLink for the file LinuxMint.iso located under /home/Test, the, I will be using the following:

ln -s ~/Test/LinuxMint.iso MintLink

To list the symbolic links in the directory, here, I have used the tree command which will also show the path by following the symlink:

create a symlink in linux

Hard links are a bit different than soft links and I personally find them more useful than soft links.

The key advantage is you can have a backup for your special files and the best part is it won't take any at all.

Let me explain.

The hard link points at the data block where the data is stored so even if you delete the actual file, you can still access it using the hardlink!

I won't go into much detail, but if you want to learn more, you can check out our detailed guide on the hard link:

Hard Link in Linux: Everything Important You Need to Know
Learn the concept of hard links in Linux and its association with inodes in this tutorial.

Now, let's jump to how you can create a hard link.

To create a hard link, all you have to do is use the ln command without any options and it will create a hard link from the given source:

ln [target-file] [link-name]

Here, I will be creating a hard link for the LinuxMint.iso inside the Tutorial directory:

ln LinuxMint.iso ~/Tutorial/LinuxMint

But unlike the soft link, the hard link looks absolutely the same as any other file.

To find the hard link, you can check the inode number of the file using the ls command with the -li flag:

how to find the hardlink in ubuntu

And as you can see, the inode numbers are exactly the same for both files!

ūüí°
You can not create a hard link pointing to the directory.

To create a soft link pointing to a directory, all you have to do is use the -s flag and  give the path to the directory (the same one you did with files):

ln -s /path/to/directory LinkName

So let's say I want to create a soft link of the Downloads directory, then, I will be using the following:

ln -s ~/Downloads Downloads
how to create a soft link pointing to the directory

And as you can see, the soft link that I created is pointing to the Downloads directory!

Think of a scenario where you want to create a soft link pointing to another soft link.

Sure you can do that!

But if you use the normal method for creating a soft link that is pointing to the soft link, the link will be created for the original file and not for the soft link itself.

So in that case, you'd want to treat that soft link as a normal file.

And this can be done using the -n option:

ln -n [existing-symlink] [symlink]

For example, here, I have created a soft link named Hello which will point to another soft link MintISO:

ln -sn MintISO Hello
create a soft link pointing to another soft link

And as you can see, the Hello soft link is only pointing to another soft link MintISO!

If you try creating the symlink with the existing file name, it will throw an error saying "Failed to create the symbolic link, file already exist"

failed to create symbolic link: File exist

To force the creation of the symlink, all you have to do is use -f flag:

ln -f [target-file] [link-name]

Here, I tried creating the symlink with the existing file name but this time, the ln command is paired with the -f flag:

force the creation of symlink

And as you can see, the symlink was created without any errors!

Finding broken symlinks is indeed a headache. Especially, if you are doing everything manually.

But no more headaches! We have a detailed guide for that topic:

How to Find Broken Symlinks in Linux
This quick tip teaches you various ways to find all the broken symlinks in Linux command line. You’ll also learn to manage those broken soft links.

I hope you will find this guide helpful.

And if you have any queries, feel free to ask in the comments.