Home / Linux/Unix/BSD / Symbolic link Linux

Symbolic link Linux

A symbolic link is a special file that provides Linux users with a reference to another file or directory. We also refer symbolic links as a soft link or symlink.

Something Here

In this guide, we’ll teach you how to create symbolic links using the ln command.

Types of Symbolic Links

Before we dive into how to create a symbolic link, you have to understand there are two types: hard links and soft links.

Both your soft link and hard link carry a relationship that answers to the inode in your Linux system.

An inode is a database that describes the directory or file attributes such as the physical location on your hard drive. You can think of the inode as the numerical equivalent of a complete address and its details.

If a file is moved to a different location in your hard drive, the inode value will also change, which directly affects hard links.

Speaking of which…

Hard links – Serves as a direct reference to a file via the inode. Hard linking applies only to the file system and is not applicable to directories.

Soft links – A soft link, on the other hand, is a shortcut that serves as a reference to a file but not via the inode. Unlike hard links, a soft link can be applied to a file and directory.

Additionally, you can create symbolic or soft links and reference these across different hard disks.

Using the ln Command

The ln command is used to create a symbolic or hard link. We use the ln command specifically for creating a link between files.

Using the ln command is also used to create both types of links: hard and soft links. By default, the ln command will create a hard link. If you want to create a symbolic link, you have to use the -s option.

Examples:

To create a symbolic link, the syntax will use this command as its format:

ln - s [options] file link

The command used for creating symlinks is as follows:

ln -s source_file symbolic link

Your “source_file” refers to the existing file in your current directory. The “symbolic link” is the name of the symlink you want to create that you want to refer to the existing file.

Let’s say you have a file named sample.txt and you want to create a symlink with the name Shortcut1. To create a symbolic link for this, type the command:

ln -s sample.txt Shortcut1

This creates a symbolic link with the name “Shortcut1” that points to the “sample.txt” file.

To verify the symlink was successfully created, use the ls -l command:

ls -l Shortcut1

As a result, you’ll be given this output:

lrwxrwxrwx 1 [Linux user] 4 Sep 2 20:05 Shortcut1 -> sample.txt

“l” refers or represents the symlink, while the “->” means that Shortcut1 is a symlink pointing to the file sample.txt.

You can also compare both your newly created symlink and original file through the  command:

ls -li sample.txt Shortcut1

As a result, you’ll be given this output:

[inode value] lrwxrwxrwx 1 [Linux user] 4 Sep 2 20:05 Shortcut1 -> sample.txt
 
[inode value] -rw-rw---- 1 [Linux user] 2 Aug 6 22:13 sample.txt

You’ll notice that the [inode value] for Shortcut1 and sample.txt are different because while sample.txt represents the file in your current directory, Shortcut1 is the symlink that points to the file in your directory, giving it a different inode value.

How to Create Symlinks to a Directory

Creating symlinks or symbolic links to a directory using the ln command is similar to creating a symbolic link to a file.

The syntax and command used will follow the same format only that the name is now the directory name instead of the file name.

For example, if you want to create a symbolic link from the /drive1/movies to ~/fave_movies directory, type the command:

ln -s /drive1/movies ~/fave_movies

Overwriting and Removing Symbolic links

Linux will prompt an error message if you create a symbolic link that currently exists in your file or directory. To overwrite this, use the -f option.

For example, if Shortcut1 already exists, then type:

ln -sf sample.txt Shortcut1

To delete a symbolic link you can use the unlink or rm command:

unlink [symlink to remove]
or
rm [symlink to remove]

Final Thoughts

Creating a Linux symbolic link requires the use of both ln and -s option. Without the -s, you’ll be creating a hard link. Take note that deleting the original source file will delete your symlink too.

We hope this article helped you understand how to create symlinks in Linux! We’d love to hear your feedback in the comments section below!