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.
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.
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”.
If you want to confirm the creation of symlink, then you can use the below-mentioned “ls” command.
You will get the output as shown below:
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.
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.
You will get the below-mentioned error.
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.
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.
You can either use the below command using the “rm” command for removing the symlink.
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.
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.