How to Create a Link to a Directory on Linux?

Links are generally the pointer to a file or directory. Linux operating system supports the built-in command line tool “ln(link)” command to create the links. These links are of two types such as “Soft” and “Hard” links. They are mainly used for creating a shortcut of a file/directory with the same name at any location where it can be accessed easily. 

This guide provides a step by step procedures to create a link to a directory on Linux.

  • How to Create a Soft Link to a Directory
  • How to Create a Hard Link to a Directory

How to Create a Soft Link to a Directory on Linux?

Soft links, also known as “symbolic” links, act as files with separated inode values to point out the source files and directories. They can be easily created by using the simple and generalized syntax of the “ln(link)” command:

Syntax:

$ ln -s [directory-name] [link-name]

In the above syntax:  

  • ln:  Represents the main “ln(link)” command to create the soft link of the directory. 
  • s: Tells that the link type is “soft,” i.e., soft link.
  • directory-name: Identifies the name of the directory.
  • link-name: Shows the link name of the targeted directory

Let’s use this syntax to create the soft link of a particular directory.

Example 1: Create a Directory Soft Link to at PWD

Specify the directory name with the “ln” command, i.e., “Downloads” and the link name that is “Downloadslink” in this format:

$ ln -s Downloads Downloadslink

The command has been executed successfully at the present working directory, “home.”

Verify the Link (Using “ln”)

To verify whether a directory’s soft link is created, use the “-v(verbose)” flag at the time of link creation. It shows the details of the “ln” command in the terminal:

$ ln -sv Downloads Downloadslink

The output confirms that the soft link “Downloadslink” of the “Downloads” directory has been successfully created.

Verify the Link (Using “ls”)

Apart from it; the user can also execute the “ls(list)” command with the combination of “grep” to filter out the content matches with “Downloads”:

$ ls -l | grep Downloads

It is verified that “Downloadslink” has been created as shown in the output.

Example 2: Create a Directory Soft Link at a Specific Location

To create a soft link to the directory at the desired location, specify its absolute path with the link name. 

For instance, the “Link1” of the “Downloads” subdirectory “Dir1” is created in the “Pictures” directory:

$ ln -s /home/itslinuxfoss/Downloads/Dir1 /home/itslinuxfoss/Pictures/Link1

The “ls” command verifies that the “ln” command has successfully created “Link1” at “Pictures” directory.

How to Delete/Remove Soft Link to a Directory on Linux?

The soft link of a directory can be removed after completing its usage using the “rm(remove)” command. It removes the specified link followed by its “-v(verbose)” flag for details:

$ rm -v Downloadslink

The “Downloadslink” has been successfully removed.

Note: Read our details guide on How to Remove/Delete Symbolic Links in Linux?

for more methods of soft link removal. 

How to Create a Hard Link to a Directory on Linux?

The hard link is the copy of source files containing the same associated parameters such as “filesize,” “file permissions,” and the “inode value.” 

Similar to files, It can not be created in a directory and generates this type of error:

$ ln Pictures Link2

This is because it creates an infinite loop of the directory structure that can not be easily traversed. On the other hand, they break the filesystem and provide an unambiguous parent directories structure. So only soft links can be created for directories.

Note: If the user wants to create the hard links of files, read our detailed guide on “How to Create Hard Links Linux?

Conclusion

Linux offers the “ln(link)” command for the creation of the “soft-link” of the directory. It specifies the “-s” flag instructing the “ln” command for creating a soft link to the particular directory. It can be created at the present working directory or the user-choice location. However, the “hard link” of a directory can not be created due to some limitations in this guide.

This guide has provided a detailed view of creating a link to a directory on Linux.