Linux Commands

Symbolic Link in Linux

Linux is one of the most stable operating systems available in the market that helps manage files and directories via command-line interface commands. You can use the Linux command-line interface command for creating, removing, or deleting a symbolic link. It also allows you to create a symbolic link called symlink or soft link and points to another file or directory. We focus on various Linux commands to get along with a symlink.

Before moving forward, you must know about various types of symbolic links and their importance in Linux. There are two types of symlink available in Linux. These are:

  • Hard Link: you can understand the hard link as the additional name given to the existing file within Linux. You can associate the hard links with two or more file names having the same inode efficiently. One or more hard links can be created for a single file. You cannot create a hard link for directories or files stored on different filesystems or partitions.
  • Soft Link: it is considered a shortcut in Windows. It is an indirect pointer for any file or directory. It is different from a hard link because you can use it for pointing files or directories in the different file systems or partitions.

Now, we are moving forward to create or delete the symlinks in Linux.

Using the “ln” Command

Using this command-line utility, you will be able to create links between the files. If you do not specify what type of link you want to create, it will create the hard link by default. So, if you want to create the soft link, specify the “-s” option after the command.

Use the below-mentioned syntax for creating links using the “ln” command.

ln -s [OPTIONS] FILE1 LINK

If both the FILE1 and LINK are available, use the “ln” command for creating a link from the file specified as (FILE1) to the file specified as (LINK).

But if you have only the first argument as FILE1 and the second argument as (.), then the link is created to that file present in the current working directory. The created symlink will have the same name as the file’s name to which it points. If the link has been created successfully, there will be no command output and will return zero.

Creating a Symlink to a File

Run the following command for creating a symlink to a file.

ln -s source_file1 symbolic_link

In the command mentioned above, specify the “source_file1” with the name of the existing file in the Linux system for which you are going to create the link and replace the parameter “symbolic_link” with the name of the symbolic link.

Here, the symbolic_link parameter is optional in the above command. If you forgot to mention any symbolic link in the command, your current directory would create a new link.

Considering the below example, we will be creating a symbolic link with the name “my_link.txt” to a file named “my_file.txt”.

ln -s my_file.txt my_link.txt

If you want to confirm the creation of symlink, then you can use the below-mentioned “ls” command.

ls -l my_link.txt

You will get the output as shown below:

lrwxrwxrwx 1 linuxize users 4 Nov 2 23:03 my_link.txt -> my_file.txt

The first character “l”, represents the file type flag with a symbolic link in the above output. The -> symbol will represent the file the symlink represents.

Creating a Symlink to a Directory

You can create the symlink for the directory the same way you create the symlink for the file. For this, mention the directory name as the first parameter in the command and provide the desired symlink as the second parameter in the command.

The example below will help create a symbolic link from the /mnt/my_drive_1/movies directory to the ~/my_movies directory.

ln -s /mnt/my_drive_1/movies ~/my_movies

Overwriting Symlinks

If you create a symlink link that exists already, you will get an error using the “ln” command. Consider the below example where we are creating the existing symlink again.

ln -s my_file_1.txt my_link_1.txt

You will get the below-mentioned error.

ln: failed to create symbolic link 'my_link_1.txt': File exists

But if you still want to overwrite the destination path of the symlink, you have to provide the “-f” option along with the “ln” command for forcefully implementing it. Consider the below example.

ln -sf my_file.txt my_link.txt

Removing Symlink

If you want to delete or remove the existing symlink, you can either use the “unlink” command or the “rm” command. Follow the below-mentioned “unlink” command syntax.

unlink symlink_to_remove

You can either use the below command using the “rm” command for removing the symlink.

rm symlink1_to_remove

But if you move the source file to a different location in the filesystem, the symbolic file will be left broken and need to be removed using the “rm” command.

Conclusion

In Linux, you can leverage the symbolic links used to link the libraries and make sure that the files are in a consistent place without the need to move or copy the original file. These links are often used to hold multiple copies of the same file but in different places referencing one file. This article has mentioned various commands that will create, delete, and overwrite the symbolic links.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.