1. Overview

In this tutorial, we’ll be discussing different ways to make symbolic links in Linux. They’re also called soft links and, together with hard links, we can create them using the ln command.

Before diving deeper, it’s good to understand at least the basics of soft and hard links.

Notably, the examples in this tutorial use commands tested in Bash and should work in all POSIX-compatible shells.

We can think of ln in terms of four forms.

The first form requires us to be explicit in naming both the target and the link we want to create:

$ ln -s file.txt link.txt

With this command, we’ve created a symbolic link called link.txt that points to the file.txt file. Using the command ls -l, we can see the result:

$ ls -l
-rw-rw-r--. 1 vagrant vagrant 10 Apr 12 15:23 file.txt
lrwxrwxrwx. 1 vagrant vagrant  8 Apr 12 15:23 link.txt -> file.txt

Further, we can create symlinks to directories in the same way:

$ ln -s dir link

This applies to the other forms as well.

Of all the forms, this is the only form in which we can create links to files located in the same directory. This becomes clear when we look at the second form.

The second form is the shortest one of all four forms. By omitting the link name, ln creates links in the current working directory that have the same name as the target.

For example, we can create a link called log in our current directory that points to /var/log:

$ ln -s /var/log

Thus, our link should look something like this:

$ ls -l
lrwxrwxrwx. 1 vagrant vagrant 8 Apr 12 15:35 log -> /var/log
...

Notably, this command won’t work if we execute it from within the /var directory. Let’s cd into /var and try again:

$ cd /var/
$ ln -s /var/log
ln: failed to create symbolic link './log': File exists

Here, our system may complain that the file already exists.

The remaining two ln forms are all about creating multiple links at once.

Let’s link a file and directory in one statement:

$ ln -s /etc/hosts /var/log /home/vagrant

With this command, we’ve created two links, respectively pointing to a file and a directory. They’re located in the target directory, in this case, /home/vagrant:

$ ls -l
lrwxrwxrwx. 1 vagrant vagrant 10 Apr 12 15:50 hosts -> /etc/hosts
lrwxrwxrwx. 1 vagrant vagrant  8 Apr 12 15:50 log -> /var/log
...

The fourth form of ln is very similar to the third. The only difference is that we now first state the directory where to create the links by using the -t parameter:

$ ln -s -t /home/vagrant /etc/hosts /var/log

The result is the same as if we had used the third form:

$ ls -l
lrwxrwxrwx. 1 vagrant vagrant 10 Apr 12 15:50 hosts -> /etc/hosts
lrwxrwxrwx. 1 vagrant vagrant  8 Apr 12 15:50 log -> /var/log
...

Essentially, this form exists mainly for convenience.

6. Conclusion

As we’ve learned, there are four forms in which we can use ln to create symbolic links.

Of all forms, the first one is the most explicit and the only form that allows linking to files or directories in the same location.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.