How to Symlink a Directory in Linux?

The term “symlink” refers to a symbolic link pointing to the original file in Linux. It is also a soft link through which the user can easily access the file from any directory. It works similarly to the shortcut that is utilized in Windows OS.

This article aims to explain various methods to symlink a directory in Linux. The supported content of this article is as follows:

  • Symlink a Directory
  • General Syntax
  • Add Symlink a Directory in Linux
  • Remove the Symlink a Directory in Linux
  • Overwrite the Symlink a Directory

Let’s start with the basic syntax of creating symlinks.

How to Symlink a Directory in Linux?

The “ln” command is utilized to create a symlink in Linux. It requires the path of the directory for access. Additionally, users can rename the newly created symlink of the directory.

General Syntax

The syntax that follows to create a symlink of the directory is given below:

$ ln -s [directory path] [name]

In the above syntax, the description is enlisted as below:

  • s: It refers to the symbolic link that you access through the path.
  • directory path: The path of the directory.
  • name: It represents the name of the symbolic link.

To explore more options of the “ln” command, you can utilize the “help” command as below:

Let’s practice the symlink with multiple examples.

Example 1: Symlink a Directory in Linux

To make a symlink of the specified directory “Office”. To display the content in the home directory, the “ls” command is utilized as below:

$ ls

We will target the “Office” directory in the list of multiple directories and make a symbolic link. 

The “ln” command is utilized with the “s” option to make a symlink of the “Office” directory. While the symbolic name of the generated link is “Soft_Office” as below:

$ ln -s Office Soft_Office

The output shows that the symlink of the “Office” directory has been created with the new name of “Soft_Office”.

An alternative way to display the list of directories with content that is present inside it is by executing the below script:

$ ls -l

The output shows that the symlink of the “Office” directory has been created in the “Soft_Office” directory.

Example 2: Remove the Symlink a Directory in Linux

To remove the symlink of the directory, the “unlink” command is utilized as below:

$ unlink Soft_Office

The output shows that the symlink of the “Soft_Office” directory has been removed along with its content.

Alternatively, users can utilize the “rm” command to remove the symlink of the directory by specifying the name:

$ rm Soft_Office

The output returns that the particular symbolic link of “Soft_Office” has been successfully removed.

Example 3: Overwrite the Symlink of a Directory in Linux

An error occurs when the user creates a symlink of the directory with the same name. For this, the “-f” option is utilized to forcefully overwrite the symbolic link of the directory:

$ ln -sf Office Soft_Office

The output shows the symlink “Soft_Office” has been overwritten from the “Office”.

That’s all to symlink a directory in Linux.

Conclusion

In Linux, the symlink of a directory is created by following the syntax “<ln -s [path of the directory] [symbolic name]>”. It is useful to access the directories without navigating to them. The symlinks can be overwritten or removed as per the user’s requirement. This article has explained multiple examples to symlink a directory in Linux.