How to Create Symbolic Links in Linux Using the LN Command

Symbolic Links are a type of link in Unix-like systems that point to a different file or directory. They are also known as “soft links” or Symlinks.

 

In this tutorial, we are going to look at how we can create and delete Symbolic Links in Linux using the ln terminal command. The instructions here will also work for other Unix-based systems such as macOS and Ubuntu.

 

In Linux, there are two types of links. Soft links and Hard Links.

 

  • Hard Links are essentially different names for the same file using the same inode. Therefore Hard Links have to be located on the same, disk, filesystem, partition or volume.
  • Soft Links or Symbolic Links are essentially nothing more than shortcuts to access a file or directory. This makes them much more flexible allowing you to create Soft Links to any location you desire. Think of them like those desktop shortcuts on Windows.

 

To create a Symbolic Link we will use the ln command. The syntax of ln starts with a set of options followed by the source file and finally the Symbolic Link.

 

ln [options] source_file symbolic_link

 

Now we know the syntax we can go ahead and create a Symlink. We want to create a Symbolic Link so we will pass in the -s flag as the option.

 

ln -s file.txt link.txt

In the above command, we have created a Symbolic link called link.txt which points to the file file.txt. The symbolic_link parameter is optional, though if you don't add it the default behaviour of ln is to create the Symlink in your current working directory.

 

You can verify the symlink was created using the ls command.

ls

 

A really useful way to use Symbolic Links is with directory paths. If you often find yourself needing to go to a directory nested deeply in your file system or on another volume you can create a shortcut to it.

 

Creating a Symbolic Link to a directory works the same way as with a file. You just need to be aware of the file structure of your system to point the Symlink to the correct location.

 

Let's say we wanted to create a shortcut to the videos folder on a mounted drive, we could use ln like this:

ln -s /mnt/volume_nyc1_01/media/videos videos

The key point is to run this command where the videos Symlink will be most convenient for you.

 

Now you just have to type ls videos to see the contents of /mnt/volume_nyc1_01/media/videos or cd videos to move to that directory.

 

You can remove Symbolic Links just like you would any other file on Linux; either with rm or unlink. Here are two quick examples of how to use them both in this case:

rm videos
unlink video

 

Just make sure you are deleting a Symlink and not an actual file by using ls -l. You can read more about the Long List Format including what the flags mean in my article about the ls command.

 

Conclusion

In this article, you have gained an understanding of “Soft” and “Hard” links in Linux are and how to create Symbolic Links using the ln utility.

command line