How to create a symlink on Linux

How to create Symbolic Links on Linux

Because everything in Linux is treated as a file, using links adds flexibility to create mappings to certain files or directories. There are hard links and soft links. A symlink, also called a symbolic link, is a file that points to another file or directory on the server. Except for files and directories, the symlinks can point to pipes or even other symlinks, which creates symbolic link chains.

When you upgrade or downgrade an application on your server, some libraries may not be compatible, causing your application to crash. To ensure library compatibility, you can use symlinks to point the generic library name to the specific library version. Symlinks are also used in the Apache and Nginx configurations, specifically to map which sites are enabled by creating a symbolic link from the sites-available to the sites-enabled directory.

How to create a symlink

Creating symbolic link in Linux is a straightforward process with the command ln -s and both paths from the source and destination directory.

ln -s /path/to/source/file /path/to/destination/file

You can find an example below for Nginx site configurations:

ln -s /etc/nginx/sites-available/my.site.conf /etc/nginx/sites-enabled/my.site.conf

You can try creating your own symbolic link in the opt directory, for example:

root@Server:/opt# ln -s /etc/someconfig.conf someconfig.conf

Unlike hard links, symlink will have its own inode number that will refer to the path of the original file. You can check the symbolic link that was created with the command:

# ls -l
someconf.conf -> /etc/someconf.conf

You can notice that this shows that the someconf.conf file is a symbolic link to /etc/someconf.conf file.

If you try to create a symlink that already exists, you will receive an error that the file already exists. You can run ln -sf to force the creation of symlink, which will unlink and create the symbolic link again with a different path:

# ln -sf /etc/new/app/someconf.conf someconf.conf

How to remove a symlink

There are two ways to remove the created symlinks, you can either use the rm command or unlink. To delete the symlink that was created previously with rm or unlink, you can use:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
rm someconf.conf 

or

unlink someconf.conf

Both will have the same effect, it will remove the someconf.conf symlink but keep the file in the source directory /etc/someconf.conf.

Previously, we mentioned Chains of Symlinks, which are symlinks of symlink. Let’s say you have the previous symbolic link. Then, you create a new symlink of that file.

ln -s /etc/someconf.conf /opt/someconf.conf
ln -s /opt/someconf.conf /var/opt/someconf.conf

Now, if you try to delete the last symlink, the head of the chain with:

unlink /var/opt/someconf.conf 

This will remove the symlink someconf.conf -> /opt/someconf.conf, but the symlink in /opt/someconf.conf will still exist. This does not apply if you delete the /opt/someconf.conf symlink instead of /var/opt/someconf.conf, then the symbolic link chain won’t exist.

Conclusion

Symlinks are often shortcuts to files or directories with long paths, simplifying complex directory structures and helping you organize your important files. After reading this article, you should know how to create new symlinks and overwrite or remove existing symbolic links.

We hope this article helped you better understand symbolic links. If it did, please let us know with a comment below. If you know of others who might be interested in it, you can also share this post online. Thank you.

Leave a Comment