1. Overview

As we’ve known, under the Linux command-line, we can create links to ordinary files. Sometimes, we may want to create links to directories.

In this quick tutorial, we’ll take a look at how to do that with the ln command.

We’ll focus on creating symbolic (soft) links instead of hard links. 

Creating a link to one directory is a common use case of the ln command. The syntax is the same as creating a soft link to a file:

ln -s TARGET_DIR LINK_NAME

Now, let’s see it in action. Let’s create a soft link /tmp/test/linked_etc directory to the directory /etc:

$ ln -s /etc /tmp/test/linked_etc
$ stat /tmp/test/linked_etc
  File: /tmp/test/linked_etc -> /etc
  Size: 4               Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46728       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
...

After we created the link using the ln command, we listed the detailed information of /tmp/test/linked_etc using the stat command.

In the output of the stat command, we can see that the symbolic link has been created successfully.

If we don’t give the LINK_NAME, the ln command will create a link with the name of the target directory under the current working directory:

$ pwd
/tmp/test

$ ln -s /etc
$ stat /tmp/test/etc
  File: /tmp/test/etc -> /etc
  Size: 4               Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46735       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
...

As the example shows, the link etc has been created under the current working directory /tmp/test.

We’ve seen how to create a link to a single directory. The ln command allows us to create links to multiple target directories in one shot.

Now, let’s take a look at the syntax to do that:

ln -s -t DIR_TO_CREATE_LINKS ​TARGET_DIR1 TARGET_DIR2 ... 

As usual, let’s understand how the command works through an example.

Let’s say we want to create two soft links under /tmp/test pointing to the Java JDK directory /usr/lib/jvm/java-15-jdk and Python directory /usr/lib/python3.9:

$ ln -s -t /tmp/test /usr/lib/jvm/java-15-jdk /usr/lib/python3.9 

$ stat /tmp/test/java-15-jdk /tmp/test/python3.9 
  File: /tmp/test/java-15-jdk -> /usr/lib/jvm/java-15-jdk
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46771       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
 ...
  File: /tmp/test/python3.9 -> /usr/lib/python3.9
  Size: 18              Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46772       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
...

As the stat output above shows, the two links are created under the directory /tmp/test and pointing to desired target directories.

Alternatively, we can create links to multiple directories in another form:

ln -s TARGET_DIR1 TARGET_DIR2 ... DIR_TO_CREATE_LINKS

This time, we don’t use the -t option. Instead, we put the DIR_TO_CREATE_LINKS directory at the end of the argument list.

An example is always a nice way to address how a command works.

Now, let’s create links to the Python and Java directories under a new directory /tmp/test2:

$ ln -s /usr/lib/jvm/java-15-jdk /usr/lib/python3.9 /tmp/test2 

$ stat /tmp/test2/java-15-jdk /tmp/test2/python3.9
  File: /tmp/test2/java-15-jdk -> /usr/lib/jvm/java-15-jdk
  Size: 24              Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46811       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
...
  File: /tmp/test2/python3.9 -> /usr/lib/python3.9
  Size: 18              Blocks: 0          IO Block: 4096   symbolic link
Device: 30h/48d Inode: 46812       Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
...

The stat output tells us that two links have been created under /tmp/test2.

4. The Permissions

So far, we’ve learned how easy it is to create soft links to directories.

And, if we read all the above outputs of the stat command carefully, we’ll see all soft links’ permission is the same: 0777/lrwxrwxrwx.

We should keep in mind that in Linux, the file system permissions of a symbolic link are not used. The permission is always 0777. 

The target file’s own permissions control the access modes of the target file or directory.

Moreover, if we change the soft link’s permission, the chmod command will forward the change to the target file or directory. No matter whether the change is successful or not, the permission of the soft link is still 0777.

Let’s understand it through an example.

First, let’s find two directories we want to link:

kent$ ls -ld /etc /home/kent/Desktop/aDirectory 
drwxr-xr-x 131 root root 12288 Mar 26 11:07 /etc
drwxr-xr-x   2 kent kent  4096 Mar 26 23:23 /home/kent/Desktop/aDirectory

Next, we’re going to create two links to the directories above under the directory /tmp/test3:

kent$ ln -s /etc /home/kent/Desktop/aDirectory /tmp/test3

kent$ ls -l /tmp/test3/*
lrwxrwxrwx 1 kent kent 29 Mar 26 23:27 /tmp/test3/aDirectory -> /home/kent/Desktop/aDirectory/
lrwxrwxrwx 1 kent kent  4 Mar 26 23:27 /tmp/test3/etc -> /etc/

Good, we’ve created the two links. Finally, let’s attempt to change the permissions of the two soft links to 700:

kent$ chmod 700 /tmp/test3/etc 
chmod: changing permissions of '/tmp/test3/etc': Operation not permitted

We cannot change the permission of the soft link /tmp/test3/etc. This is because the chmod command will apply the permission change to the target directory /etc.

Apparently, as a regular user “kent”, we’re not permitted to change the permission of /etc.

Next, let’s see what’ll happen if we change the permission of the other link:

kent$ chmod 700 /tmp/test3/aDirectory

kent$ ls -ld /home/kent/Desktop/aDirectory
drwx------ 2 kent kent 4096 Mar 26 23:23 /home/kent/Desktop/aDirectory/

As the output shows, the permission of the target directory /home/kent/Desktop/aDirectory has been changed from “drwxr-xr-x” into “drwx——“.

In the end, let’s check if the two chmod commands will affect the permission of the two soft links:

kent$ ls -l /tmp/test3/*
lrwxrwxrwx 1 kent kent 29 Mar 26 23:27 /tmp/test3/aDirectory -> /home/kent/Desktop/aDirectory/
lrwxrwxrwx 1 kent kent  4 Mar 26 23:27 /tmp/test3/etc -> /etc/

Therefore, we cannot change the permission of a soft link — it’s always 777.

5. Conclusion

In this article, we’ve learned how to create soft links to directories through examples.

Further, we’ve discussed the permissions of soft links and the implications of trying to change them using chmod.

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