How to Create “Symlinks” (Symbolic Links) In Your File System

How to create symlinks in your file system for Linux / BSD / GNU

All InMotion Hosting plans allow you to create symbolic links (or, symlinks) on the command line. All you have to do is access your server via SSH. The only caveat here is that you create links to files you have access to. For example, you cannot create links that would require root access — unless you have a VPS plan or above and have obtained root access to that server. (Learn about the differences between VPS hosting vs dedicated server.)

The most basic operation of ln -s requires only two arguments:

  1. Target
  2. Link name

The resulting process will be the creation of a linked file that will point to the target. In essence, both files will be identical, but one will merely be a reference to the other.

It is important to understand these concepts before proceeding on to making your own links. As you have no doubt realized, there are many different ways to achieve the same outcomes in any Unix-like operating system. So why create links instead of doing something else? Better to learn in advance what this operation intends to accomplish before determining whether or not it is the appropriate action.

By creating symlinks, you are in effect taking one file and mirroring it to another. To put it another way, you are taking information passed to a linked file and sending it to a target file.

The basic construction of creating symlinks follows this pattern:

ln -s <target file> <link file>

This can be confusing because the information does not pass from left to right, but rather from right to left. Information passed to the link file will take effect on the target file. Modifications made to the target file will stay there.

How to Create Symlinks

Before creating symlinks it’s important to understand how the direction of the link works. The link will be created from the second file to the first file provided to ln.

ln -s file1 file2

The example above links file2 -> file1. If file2 already exists, ln will throw an error. The ln program creates the linked file as well as the link itself.

Just like when linking files on your website, you can give relative or absolute links. A relative path allows you to give a different path to the file based on different circumstances rather than an absolute path which, as its name suggests, will never change.

But why would you want to create a relative symlink? Imagine you are working with the same file on different machines, and the path to that file is different on each machine. You can create a relative symlink in your scripts, so that you can keep referencing the same file in both places.

You can create a relative symlink by passing the -r option.

ln -rs <target> <link>

In general, absolute links where possible can save you some trouble down the road if you have to do some troubleshooting. The -r option is exactly that, an option, if you need it.

In most every day cases the -s option will be enough.

For those who are interested, below you will find some additional information about how the ln program works, and some other issues you might run into while using this command.

The default behavior of ln (that is, without the -s option) creates a hard link to a file. This means that the target and the linked file are in essence the same — but not exactly. This can create some complications, and so is generally not recommended. You are best advised to use the symlinks option (-s) instead of creating hard links.

If you find that you are unable to create symlinks but are quite sure you are using the ln program correctly there may be a few reasons why problems persist. Check to make sure that the link name points to an actual target file. You may also need to check and see that you have the necessary privileges to create a link in the file system, and in the desired location.

Also, some symlinks behave differently depending on your server environment. Links created in a GNU/Linux system will not always function in exactly the same way as they would in a BSD system.

If you continue to get an error when trying to create symlinks it’s possible your system configuration does not allow it. At this point it would be best to check with an IT person, system administrator, or, depending on the situation, your hosting support.

Here are a few good options you might want to take note of when crafting symlinks:

As mentioned the section above, a link file will be created if it does not exist already, and ln will throw an error if a link file already exists. If you think about it, this makes a lot of sense. If you have a file2 that exists on your system and it has information in it, but you tell the computer to point that file to file1, the computer will say, “No you can’t do that because file2 has information in it and will be overwritten.” This can save you from data loss. But if you want to go ahead with that operation, and you want to save the step of having to go back and erase file2, there’s an option for that. If you use the -f option, you can force overwrite of file2, effectively using it as a link name to the target (file1).

Here is the ln command using the -s (soft) and -f (force) options:

ln -sf file1 file2

Notice you can put the options together, or in different places on the line, and the command will still work as expected:

ln -s file1 file2 -f

But the latter is not as aesthetically pleasing.

The ln utility also accepts a -v for verbose output. This gives you some additional output to make sure that you have created the desired link properly. It accomplishes this by printing out to you the name of each file after successfully linking the files. Check out the example below.

The command given the -s (soft), -r (relative, to denote the creation of a relative link), and -v (verbose) options:

ln -srv /a/file /tmp

And here is result printed back to the standard output:

'/tmp/file' -> '../a/file'

Creating symlinks is a great way to simplify your life when writing scripts, dealing with your server file system, or just to create convenient pathways to files you use frequently on various machines. And it may be part of your essential maintenance if you use a VPS cloud server.

Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Let us know!