How to create symlink in Linux

Symbolic links (also known as symlinks or soft links) are one of two types of links that you can create on a Linux system. If you’re just now learning about symbolic links, it may help to think of them as “shortcuts,” a term commonly used by Windows systems to represent basically the same thing.

Symbolic links are used to link to hard links. If you’re interested in learning more about hard links and how they compare to symbolic links, check our guide on creating hard and soft links. Suffice it to say that symlinks are just entries in the file system that point to files or directories. They’re mostly used for convenience.

In this guide, we’ll run through the step by step instructions of creating and removing symbolic links. You can follow along with our examples below on your own command line to get a feel for how they work.

In this tutorial you will learn:

  • How to create and remove symbolic links

Creating and testing a symbolic link in Linux

Creating and testing a symbolic link in Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Create and remove symbolic links



The ln (link) command is used to create links, and the -s option specifies that we wish to make a symbolic link. We also need to supply two arguments: the file/directory we want to link to, and the file which links to it. Here’s an example.

  1. First, let’s create a simple text file that we can link to for an example.
    $ echo "this is an example" > /home/linuxconfig/example.txt
    
  2. Next, let’s create a symbolic link to this file. We’ll place the link inside the /tmp directory.
    $ ln -s /home/linuxconfig/example.txt /tmp/example.txt
    
  3. Let’s take a look at our newly created symbolic link with the ls command.
    $ ls -l /tmp/example.txt
    lrwxrwxrwx 1 linuxconfig linuxconfig 29 Sep  8 18:08 /tmp/example.txt -> /home/linuxconfig/example.txt
    

Upon viewing our link with ls -l, you’ll see the first letter is l, indicating that this file is a symbolic link. It also shows us where the link points to, in our case /home/linuxconfig/example.txt.

We can now use this symbolic link to access the file. For example:

$ cat /tmp/example.txt 
this is an example

That’s really all there is to it. The process works the same for directories. However, before you go, there are a few more things you should know about symbolic links.



  • To remove a symbolic link, just use the rm command like you would to delete a file. You can also use the unlink command.
  • When a file or directory is deleted, the symbolic links that linked to that file or directory will remain, although they are now “broken.”
  • Keep in mind that the ln command stores the actual path provided as a string. If not within the same directory, in many cases you must provide the full path to the file or directory in order for the symbolic link to work.
  • Symbolic links don’t have permissions that you can modify. Anyone can see where the symbolic link points to but only those with permission to access the original file can read, write, or execute it (depending on what permissions they have).

Conclusion

Symbolic links are nothing more than shortcuts, whose main function is that of convenience. In this guide, we saw how to create and remove symbolic links through command line examples. We also gave a brief list of things to keep in mind when it comes to the creation of symbolic links, such as the recommendation to provide absolute paths.



Comments and Discussions
Linux Forum