Linux - Create and Manage Soft Links

Linux - Create and Manage Soft Links

In this blog, we will look into how to create and manage soft links in Linux.

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

Know how when you install a program on Windows, you might get a shortcut on your desktop? You double click on that shortcut and 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. So the double click on that shortcut basically 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, with a path to a file or directory inside.

The syntax of the command to create a soft link (also called symbolic link) is the same as before, but we add the -s or –symbolic option:

ln -s path_to_target path_to_link_file

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 long listing 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:

ln -s Pictures/family_dog.jpg picture_shortcut

Why would this be useful? If we stored an absolute path here with a command like

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

will usually show 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 softlink to directories:

ln -s Pictures/ shortcut_to_directory

Or you can softlink to files/directories on a different filesystem.

To learn more about Linux, checkout our hands-on Linux course here

To get free hands-on experience on System Administration and DevOps tasks please check out our KodeKloud Engineer program here