Celebrate Our 22nd Anniversary with Huge Savings! Up to 70% Off

What is symlink? How to add and remove symlink in a Linux server?

A symbolic link, also known as a symlink/soft link, is a file that stores the path to an existing target, which can be a file or directory on any local or external volume. When a user, application, or system call tries to access the symlink, the operating system automatically interprets the stored path and provides the contents of the target.

Note: Symlinks have a conceptual similarity with Windows shortcuts. However, the system does not automatically resolve shortcuts, making them readable only by specific programs like Windows Shell and file browsers.

What does a SymLink do?

A symbolic link directs to the original file on your system but does not store data in the target file. Therefore, if the target file is deleted, the symbolic link becomes unusable. However, removing the symlink does not impact the target file.

Benefits of Symbolic Links

Symbolic links offer numerous advantages, enhancing reliability and efficiency in their usage.

1) It is more efficient in connecting files throughout the file system.

2) Symlinks have a unique feature in creating a linked file for a directory.

3) It creates multiple entry points for a file without the need for original access copies.

4) The kernel looks at the linked file's name and goes straight to the original file, pretending to move through the user's namespace.

Drawbacks of Symbolic Links

Symbolic links come with certain disadvantages in terms of usage.

1) The most disadvantage of a symlink is that it does not directly link to a file.

2) There is no use of the symlink file if the original file gets deleted or changed.

3) There is a possibility that symlink can be dead or dangling after creating multiple files.

Difference between hard link and symlink

 

symlink

Hard link

Inode Number

symlinks have distinct inode numbers.

Hard links share the same inode number.

File Creation

symlinks can be created for both files and directories.

Hard links cannot be created for directories.

Data

symlinks can only be used as long as the original files and directories exist.

Hard links can be used even after the deletion of the file.

File System

symlinks can be utilized across the file system.

Hard links cannot be used across the file system.

File Permission

The original file permission (-rw-r–r–) and link file permission (lrwxrwxrwx) differ in symlinks.

Both files have the same permissions in hard links.

As noted above, the differences between soft and hard links originate from how they link to their targets. A soft link directs to the target's path, whereas a hard link directs to the target's data.

Note: In Linux, every file serves as a hard link to the physical location of its data.
Creating hard links produces two distinct files pointing to the same volume of data. This characteristic of hard links leads to two outcomes:

  • Modifications made to one file are reflected in all the hard links.
  • Deleting the target file does not impact the other copies.

Creating Symbolic Links in Linux

There are various ways to create symlinks based on your use case. The basic syntax for creating a symlink is as follows:

ln -s </target-directory/target-file> </symlink-directory/example-symlink>, the -s switch is crucial as it instructs the Linux ln command to create a symlink instead of a hard one. The </symlink-directory/> is optional, and if not specified, the soft link is created in the current working directory.

Creating a symlink for a File

To create a symlink to a file, open a terminal window & execute the below-mentioned command:

ln -s [target] [symlink]

The -s option directs ln to create a symlink. Without any options, the command would create a hard link.

[target] is the file that the link points to.

[symlink] is the location where the link is saved. The command places the symbolic link in the current working directory if this element is not specified.

The example below illustrates the creation of a symbolic link named "slinkfile.txt," pointing to the target file "file1.txt" in the "root" directory.

ln -s root/file1.txt slinkfile.txt

The command yields no output, but using the ls command indicates the creation of link-file.txt:

We include the -l option in the ls command to view the properties of link-file.txt:

ls -l slinkfile.txt

The letter "l" in the permissions block indicates that the file is a symlink. Additionally, the output includes the path to the target file.

Creating a symlink for a Directory

A symlink can point to a directory's absolute or relative path. To create a symlink to a directory in Linux, use the following syntax:

ln -s [target-directory] [symlink]

ln -s temp slinkdir

The ls command displays the newly created symlink in the home directory.

The ls -l output displays the "l" symbol in the permissions block & the path to the target directory.

Note: If the system is connected to another computer, such as a remote server or corporate network, symlinks can reference resources on those remote systems.

Overwriting Symbolic Links

Step 1: If an attempt is made to create a symbolic link that already exists, an error will be displayed:

The error message suggests that a file with the same name already exists in the destination. To proceed and force the system to overwrite the destination link, you can use the -f option:

ln -sf [target] [destination]

Warning: The use of the -f option results in the permanent deletion of the existing file.

Removing Symbolic Links

If a symbolic link is broken or no longer needed, it can be removed using the unlink or rm command:

unlink [symlink]

rm [symlink]

Replace [symlink] with the path to the symbolic link.

The "rm" command will confirm before removing the symlink, and "unlink" will remove the symlink without confirmation. Both commands do not provide any output if the operation is successful.

Conclusion!

In conclusion, a symbolic link, or symlink, is a powerful tool in Linux that allows users to create shortcuts or references to files and directories.

Adding a symlink involves using the "ln" command, specifying the source file or directory and the desired link location.

To remove a symlink, you can use either the "rm" or "unlink" command, where "rm" provides an option for confirmation before removal, while "unlink" removes the symlink without confirmation.

Symlinks offer flexibility and convenience in managing file paths and structures on a Linux server.


Was this answer helpful?

« Back

chat