Tutorial

Command Line Basics: Symbolic Links

Updated on November 10, 2021
author

joshtronic

Command Line Basics: Symbolic Links

Introduction

Symbolic links allow you to link files and directories to other files and directories. They go by many names including symlinks, shell links, soft links, shortcuts, and aliases. From the user’s perspective, symbolic links are very similar to normal files and directories. However, when you interact with them, they will actually interact with the target at the other end. Think of them like wormholes for your file system.

This guide provides an overview of what symbolic links are and how to to create them from a Linux command line using the ln command.

Prerequisites

To follow along with this guide, you will need access to a computer running a Linux-based operating system. This can either be a virtual private server which you’ve connected to with SSH or your local machine. Note that this tutorial was validated using a Linux server running Ubuntu 20.04, but the examples given should work on a computer running any version of any Linux distribution.

If you plan to use a remote server to follow this guide, we encourage you to first complete our Initial Server Setup guide. Doing so will set you up with a secure server environment — including a non-root user with sudo privileges and a firewall configured with UFW — which you can use to build your Linux skills.

Setting Up Example Directories and Files

The system call necessary to create symbolic links tends to be readily available on Unix-like and POSIX-compliant operating systems. The command we’ll be using to create the links is the ln command.

You’re welcome to use existing files on your system to practice making symbolic links, but this section provides a few commands that will set up a practice environment you can use to follow along with this guide’s examples.

Begin by creating a couple directories within the /tmp/ directory. /tmp/ is a temporary directory, meaning that any files and directories within it will be deleted the next time the server boots up. This will be useful for the purposes of this guide, since you can create as many directories, files, and links as you’d like without having to worry about them clogging up your system later on.

The following mkdir command creates three directories at once. It creates a directory named symlinks/ within the /tmp/ directory, and two directories (one named one/ and another named two/) within symlinks/:

  1. mkdir -p /tmp/symlinks/{one,two}

Navigate into the new symlinks/ directory:

  1. cd /tmp/symlinks

From there, create a couple sample files, one for both of the subdirectories within symlinks/. The following command creates a file named one.txt within the one/ subdirectory whose only contents are a single line reading one:

  1. echo "one" > ./one/one.txt

Similarly, this command creates a file named two.txt within the two/ subdirectory whose only contents are a single line reading two:

  1. echo "two" > ./two/two.txt

If you were to run tree at this point to display the contents of the entire /tmp/symlinks directory and any nested subdirectories, its output would look like this:

  1. tree
Output
. ├── one │ └── one.txt └── two └── two.txt 2 directories, 2 files

Note: If tree isn’t installed on your machine by default, you can install it using your system’s package manager. On Ubuntu, for example, you can install it with apt:

  1. sudo apt install tree

With these sample documents in place, you’re ready to practice making symbolic links.

By default, the ln command will make hard links instead of symbolic, or soft, links.

Say you have a text file. If you make a symbolic link to that file, the link is only a pointer to the original file. If you delete the original file, the link will be broken as it no longer has anything to point to.

A hard link is instead a mirror copy of an original file with the exact same contents. Like symbolic links, if you edit the contents of the original file those changes will be reflected in the hard link. If you delete the original file, though, the hard link will still work, and you can view and edit it as you would a normal copy of the original file.

Hard links serve their purpose in the world, but they should be avoided entirely in some cases. For instance, you should avoid using hard links when linking inside of a git repository as they can cause confusion.

To ensure that you’re creating symbolic links, you can pass the -s or --symbolic option to the ln command.

Note: Because symbolic links are typically used more frequently than hard links, some may find it beneficial to alias ln to ln -s:

  1. alias ln="ln -s"

This may save only a few keystrokes, but if you find yourself making a lot of symbolic links this could add up significantly.

As mentioned previously, symbolic linking is essentially like creating a file that contains the target’s filename and path. Because a symbolic link is just a reference to the original file, any changes that are made to the original will be immediately available in the symbolic link.

One potential use for symbolic links is to create local directories in a user’s home directory pointing to files being synchronized to an external application, like Dropbox. Another might be to create a symbolic link that points to the latest build of a project that resides in a dynamically-named directory.

Using the example files and directories from the first section, go ahead and try creating a symbolic link named three that points to the one directory you created previously:

  1. ln -s one three

Now you should have 3 directories, one of which is pointing back to another. To get a more detailed overview of the current directory structure, you can use the ls command to print the contents of the current working directory:

  1. ls
Output
one three two

There are now three directories within the symlinks/ directory. Depending on your system, it may signify that three is in fact a symbolic link. This is sometimes done by rendering the name of the link in a different color, or appending it with an @ symbol.

For even greater detail, you can pass the -l argument to ls to determine where the symbolic link is actually pointing:

  1. ls -l
Output
total 8 drwxrwxr-x 2 sammy sammy 4096 Oct 30 19:51 one lrwxrwxrwx 1 sammy sammy 3 Oct 30 19:55 three -> one drwxrwxr-x 2 sammy sammy 4096 Oct 30 19:51 two

Notice that the three link is pointing to the one directory as expected. Also, it begins with an l, which indicates it’s a link. The other two begin with d, meaning that they are regular directories.

Symbolic links can also contain symbolic links. As an example, link the one.txt file from three to the two directory:

  1. ln -s three/one.txt two/one.txt

You should now have a file named one.txt inside of the two directory. You can check with the following ls command:

  1. ls -l two/
Output
total 4 lrwxrwxrwx 1 sammy sammy 13 Oct 30 19:58 one.txt -> three/one.txt -rw-rw-r-- 1 sammy sammy 4 Oct 30 19:51 two.txt

Depending on your terminal configuration, the link (highlighted in this example output) may be rendered in red text, indicating a broken link. Although the link was created, the way this example specified the path was relative. The link is broken because the two directory doesn’t contain a three directory with the one.txt file in it.

Fortunately, you can remedy this situation by telling ln to create the symbolic link relative to the link location using the -r or --relative argument.

Even with the -r flag, though, you won’t be able to fix the broken symbolic link. The reason for this is the symbolic link already exists, and you won’t be able to overwrite it without including the -f or --force argument as well:

  1. ln -srf three/one.txt two/one.txt

With that, you now have two/one.txt which was linked to three/one.txt which is a link to one/one.txt.

Nesting symbolic links like this can quickly get confusing, but many applications are equipped to make such linking structures more understandable. For instance, if you were to run the tree command, the link target being shown is actually that of the original file location and not the link itself:

  1. tree
Output
. ├── one │ └── one.txt ├── three -> one └── two ├── one.txt -> ../one/one.txt └── two.txt 3 directories, 3 files

Now that things are linked up nicely, you can begin exploring how symbolic links work with files by altering the contents of these sample files.

To get a sense of what your files contain, run the following cat command to print the contents of the one.txt file in each of the three directories you’ve created in this guide:

  1. cat {one,two,three}/one.txt
Output
one one one

Next, update the contents of the original one.txt file from the one/ directory:

  1. echo "1. One" > one/one.txt

Then check the contents of each file again:

  1. cat {one,two,three}/one.txt
Output
1. One 1. One 1. One

As this output indicates, any changes you make to the original file will be reflected in any of its symbolic links.

Now try out the reverse. Run the following command to change the contents of one of the symbolic links. This example changes the contents of the one.txt file within the three/ directory:

  1. echo "One and done" > three/one.txt

Then check the contents of each file once again:

  1. cat {one,two,three}/one.txt
Output
One and done One and done One and done

Because the symbolic link you changed is just a pointer to another file, any change you make to the link will be immediately reflected in the original file as well as any of its other symbolic links.

Conclusion

Symbolic links can be incredibly useful, but they do have certain limitations. Keep in mind that if you were to move or delete the original file or directory, all of your existing symbolic links pointed to it will become broken. There’s no automatic updating in that scenario. As long as you’re careful, though, you can find many uses for symbolic links as you continue working with the command line.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
joshtronic

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
2 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I guess this was a typo…

alias ls="ln -s"
KFSys
Site Moderator
Site Moderator badge
December 21, 2024

Heya,

if someone wants to check more on the topic, here are some additional insights on the topic

1. Updating Existing Symbolic Links: To modify an existing symbolic link to point to a new target without deleting and recreating it, you can use the ln command with specific options:

ln -sfn new_target_link existing_symlink
  • -s: Creates a symbolic link.
  • -f: Forces the removal of the existing symbolic link.
  • -n: Treats the destination as a normal file if it’s a symbolic link to a directory.

This approach streamlines the process of updating symlinks.

  • Relative vs. Absolute Paths:

    • When creating symbolic links, consider using relative paths if the linked files or directories are likely to move within the same directory structure. Relative paths make the symlink more portable and reduce the chance of it breaking if the directory is moved.
  • Security Considerations:

    • Be mindful of security when using symbolic links. For example:
      • Ensure the symlink does not unintentionally expose sensitive files.
      • Avoid creating symlinks in directories with world-write permissions unless absolutely necessary.
    • Proper permissions on the parent directory can prevent unauthorized users from creating malicious symlinks.
  • Symbolic Links on Different Filesystems:

    • Symbolic links behave differently across filesystems. For example:
      • Symlinks created on a local filesystem may not work on remote-mounted filesystems like NFS or CIFS unless the target is accessible from the remote system.
  • Detecting and Managing Broken Symlinks:

    • To find broken symlinks in a directory, use:
find /path/to/directory -xtype l
      • This can help you quickly identify and fix any symlinks pointing to non-existent targets.
  • Symbolic Links vs. Hard Links:

    • It’s worth reiterating the differences:
      • Symbolic links: Pointers to the original file or directory. They work across filesystems and can point to non-existent targets.
      • Hard links: Direct references to the file content. They must reside on the same filesystem and cannot link to directories.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.