How to Create a Soft (Symbolic) Link in Linux

In Linux, a link is a reference to a file or directory that exists in the file system. There are two types of links: hard links and soft/symbolic links. A hard link is a direct reference to a file's inode, while a soft link is a file that points to another file or directory by name. Links are useful for creating shortcuts to frequently used files or directories and for organizing files in a logical manner.

This blog will look into how to create and manage soft links in Linux.

To learn more about hard links in Linux, check out this blog post: Linux - Create and Manage Hard Links.

When you install a program on Windows, you might get a shortcut on your desktop. If you double-click on that shortcut, that application gets launched. The application is obviously not installed on your desktop. It may have its files stored in C:\Program Files\MyCoolApp directory. And when you double-click the shortcut, this only points to an executable file at C:\Program Files\MyCoolApp\application.exe.

Simply put, double-clicking on that shortcut redirects you to the file C:\Program Files\MyCoolApp\application.exe, which gets executed.

Soft links in Linux are very similar. A hard link pointed to an inode. But a soft link is nothing more than a file that points to a path instead. It’s almost like a text file containing a path to a file or directory.

💡
Soft links are also called symbolic links. The two can be used interchangeably.

Below is the syntax for creating a soft link:

ln -s path_to_target path_to_link_file

Below is a breakdown of the command above:

  • -s: A flag indicating it is a soft/symbolic link
  • path_to_target: Our soft link will point to this path (location of a file or directory)
  • path_to_link_file: Our soft link file will be created here

For example, to create a symbolic link that points to the /etc/fstab file, we can enter:

ln -s /etc/fstab fstab_shortcut

Now if we list files and directories in a long list format, we’ll see:

ls -l

The l shows us that this is a soft link. And ls -l even displays the path that the soft link points to.

If this path is long, ls -l might not show the entire path. An alternative command to see the path stored in a soft link is:

readlink path_to_soft_link

So in our case, it would be:

readlink fstab_shortcut

You may also notice that all permission games:x:20:bits, rwx (read, write, execute) seem to be enabled for this file. That’s because the permissions of the soft link do not matter. If you’d try to write to “fstab_shortcut”, this would be denied because the permissions of the destination file apply, and /etc/fstab does not allow regular users to write here.

In our first command, ln -s /etc/fstab fstab_shortcut, we used an absolute path. But we can also use relative paths. For example, if our current directory is /home/alex, we could create a “shortcut” to our dog picture with the following comand:

ln -s Pictures/family_dog.jpg picture_shortcut

Why would this be useful? Assume we stored an absolute path here with this command:

ln -s /home/alex/Pictures/family_dog.jpg absolute_shortcut

If we ever change the directory name “alex” in the future, to something else, this soft link will break.

That’s because when someone tries to access /home/alexander/absolute_shortcut, they get redirected to /home/alex/Pictures/family_dog.jpg. The alex directory changed to alexander, so the soft link points to the old, non-existent location.

But the picture_shortcut that has a relative path to Pictures/family_dog.jpg works. That’s because when someone tries to read /home/alexander/picture_shortcut, they get redirected to Pictures/family_dog.jpg, relative to the directory where the soft link is. The soft link is in /home/alexander/, it points to Pictures/family_dog.jpg so it still reaches the intended file.

ls -l

The command above shows you broken soft/symbolic links in a way similar to this:

Since soft links are nothing more than paths pointing to a file, you can also soft-link to directories:

ln -s Pictures/ shortcut_to_directory

Or you can soft-link to files/directories on a different filesystem.

Looking to gain or polish your Linux skills? Check out our Linux Basics Course & Labs

Learning Linux Basics Course & Labs | KodeKloud

Below are the benefits of using Soft links in Linux:

  • They provide a way to create a shortcut to a file or directory, making accessing frequently used files or directories easier.
  • Soft links also allow for easy file or directory management, as they can be deleted or moved without affecting the original file or directory.
  • Soft links can be used to link files or directories across different file systems, allowing users to access files or directories from different locations without having to copy them.

Below are a few best practices to remember when using soft links in Linux.

  • Use relative paths instead of absolute paths when creating soft links. This ensures that the link will still work even if the original file or directory is moved to a different location.
  • Avoid creating circular links, which can cause problems with file access and management.
  • Periodically check and update any soft links that may have become broken due to changes in file or directory names or locations.

By following these best practices, you can ensure that your soft links are reliable and easy to manage.

Test your Linux expertise with our FREE, fun, and challenging set of hands-on challenges.

Linux Challenges | KodeKloud

Conclusion

In this article, you’ve learned how to create soft links using ln-s command. Follow the outlined best practices to ensure your soft links work as expected. Pay attention to the syntax when creating soft links to avoid link breakdown.

Want to gain a deeper understanding of Linux's other main concepts? Watch this video.


More on Linux: