Skip to Sidebar Skip to Content

How to Create a Soft Link in Linux

Linux - Create and Manage Soft Links

Highlights

  • Core Function: Soft links act like Windows shortcuts. They point to the path of another file or directory rather than the underlying inode.
  • Command Syntax: The primary tool used to Linux create soft links is the ln -s command.
  • Absolute vs. Relative: Use relative paths when creating links to prevent them from breaking if the parent directory is moved or renamed.
  • Cross-Filesystem: Unlike hard links, you can easily Manage symlinks Linux across different filesystems or partitions.
  • Essential Toolkit: Understanding link management is a foundational skill when mastering core Linux file system commands.

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 logically organizing files.

This blog will look into how to Linux create soft links and manage them effectively. To learn about the Hard link vs soft link differences in detail, read this blog: 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 launches although the install is not 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 executes. Linux symbolic links are very similar. A hard link points 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 a practical ln command 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 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:

Soft Links

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. To efficiently Manage symlinks Linux users simply rely on standard removal commands like rm.
  • 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, especially when combining them with other Linux file system commands:

  • 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.

Enroll in our Linux Basics Course to learn more Linux concepts.

Learning Linux Basics Course & Labs | KodeKloud

Conclusion

In this article, you've learned how to Linux create soft links using the 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.


More on Linux:


FAQs

Q1: What is the main Hard link vs soft link difference?

A hard link acts as a direct reference to the file's underlying inode (meaning the original file and hard link are indistinguishable and share the exact same data blocks). A soft link is essentially a shortcut file containing the text path to the target.

Q2: Can I use the ln command to link a directory?

Yes, but only with Linux symbolic links (ln -s). You cannot create a hard link to a directory due to file system restrictions designed to prevent infinite recursive loops.

Q3: What happens if I delete the original target file?

If you delete the target file, the soft link becomes a "broken" or "dangling" link. It will still exist in the directory, but attempting to access it will result in a "No such file or directory" error.

Q4: How do I delete a soft link?

To Manage symlinks Linux provides standard tools like rm or unlink. Using rm symlink_name removes the shortcut itself, leaving the original target file completely untouched.

Q5: How do I see where a soft link points?

You can use the ls -l command, which is one of the most common Linux file system commands, to view the file details. It will show an arrow indicating the target path (e.g., link_name -> target_file). Alternatively, you can use a quick ln command example extension by typing readlink link_name.

Mumshad Mannambeth Mumshad Mannambeth
Mumshad is passionate about sharing his knowledge on DevOps and Cloud & Automation technologies. He believes the best way to learn is to learn by doing and in a fun way.

Subscribe to Newsletter

Join me on this exciting journey as we explore the boundless world of web design together.