How to Create Symbolic or Soft Link in Linux

Creating symbolic or soft links in Linux is pretty straightforward, but before looking at how to create symbolic or soft link in Linux, let us discuss what they are and why to use them, and all types of links.

A symbolic link, sometimes known as the soft link, is a pointer pointing towards a file or directory in the UNIX or Linux operating system created using the ln command. Similarly, windows users create shortcuts of files or directories from the menu context.

Two Types of links

Difference between symbolic or soft links and hard links
Difference between symbolic or soft links and hard links

There are multiple types of links, like soft links and hard links. The difference between them is that soft links store the address or location as a string. If a file is moved or deleted soft links stop working. While a hard link creates another copy of the exact file, even if the hard link source file is deleted, we can still able to access it.

How to create .desktop shortcut Icon to App Manager in Linux

Today, we look at how we can easily create, update, rename, and remove symbolic or soft links.

Creating Symbolic or soft links to files and directories

Soft links can be easily created using the built-in command ln Which comes by default with all UNIX or Linux distributions like Debian, Ubuntu, RedHat, CentOS, Arch, etc.

Syntax

$ ln -s [/path/to/file_or_directory] [path/to/symlink]

Let’s create a soft link of file.txt on a desktop. So, it can be easily accessed without going to the abstract location of that file.

$ ln -s /home/trendoceans/Documents/file.txt /home/trendoceans/Desktop/file.txt

The original file is located at /home/trendoceans/Documents/file.txt, but now we created a soft link at /home/trendoceans/Desktop/file.txt so that we can now easily access file.txt from the desktop soft link.

Now, if you use the ll command to check all files and directories in your current location or where you created your soft links, you can easily spot that the soft link refers to the source file’s actual path.

ll to check soft links
ll to check soft links

There is another way to check the type of the file or directories using a built-in command file.

file command to check file type
file command to check the file type

Editing Symbolic or Soft Links

Whether you modify a file from its source or via a soft link, it will reflect its source file directly. For example: if I change the content of file.txt from “Hello TRENDOCEANS” to “TRENDOCEANS” from its soft link, it will directly reflect its original file.

Modifying soft links
Modifying soft links

Rename Symbolic or Soft Links

Soft links can be easily renamed to something new without affecting anything. But make sure not to change the source file name; otherwise, soft links will navigate to an already destroyed file.

Removing or Unlinking Soft Links

There are different ways to remove soft links. One is to use the unlink command and specify the path of the soft link you want to remove.

$ unlink file.txt
Removing or unlinking soft links
Removing or unlinking soft links

Final Thoughts

Symbolic or soft links are a convenient and valuable tool for web developers and DevOps engineers. I hope you learned something new lets us share your opinion in the comment box.

Leave a Reply