Linux Commands

ln Command in Linux

To link to files or directories, use the ln command. Link is abbreviated as (“ln”). The Linux command line or the shell, which may be opened and used by using a terminal window, is given the command. It can be used to construct soft or hard links and is one of the most significant and often used terminal commands. We will discuss how to establish symbolic links using the ln command in this manual. However, we must first comprehend the two categories of linkages as follows:

A hard link might be viewed as a different name for a current file, associating several file names with a single node is known as creating hard links. For one file, you can establish one or more hard links. Directories and files on different filesystems or partitions cannot be linked with hard links.

Soft links resemble Windows shortcuts in certain ways. It is a hint that points indirectly to a file or location. An alternative filesystem or partition can be referenced through a symbolic link, but a hard link cannot.

Syntax of ln Command in Linux in Ubuntu 20.04

Symlinks between files can be made with the ln command. Hard links are automatically created on a file using this command. But you may also make symbolic links by employing the (-s or -symbolic) option. The following is the syntax for the ln command when making symbolic links:

ln [flags] file_name link_name_file

The second parameter (link-name) to the ln command builds a link from the provided file or file-name. It will, however, construct a link of the supplied file into the current directory if a second argument is not provided or if only (.) is used as a second argument.

How To Implement the ln Command in Linux in Ubuntu 20.04

The creation of links in Linux will now be demonstrated using a few straightforward examples. For significant routine operations that occur frequently, the command syntax is appropriate. The ln command can also be used to implement more complicated linking procedures. However, these call for more complicated code and will require some command-line knowledge.

Example # 1: Using the ln Command for the Creation of a Hard Link file in Linux

A file may have one or more hard links created within it. Files and folders on a different disc or filesystem cannot have hard links created for them. Utilizing the ln command is the easiest method for generating hard links. To build the hard link, issue the following command:


In the Ubuntu shell, we have ln with the file, “data_file.txt”. We linked that file by creating the “mylink_file.txt”. When we have given an “ls” command, the “my_link.txt” is created and located in the same folder where the “data_file.txt” is placed.

Example # 2: Using the ln Command for the Creation of a Symbolic Link File in Linux

An indirect file pointer is a symbolic link. In contrast to hard links, symbolic links can point to a specific specified directory on a distinct filesystem or partition. Use the -s option together with the ln command to generate a symbolic or soft link, as seen below:

We have used the “ln” command with the “-s” flag. With the help of this “-s” flag, we have created the symbolic link “link_file2.txt”. The “ls” command is given to display the symbolic link created.

Example # 3: Using the ln Command for the Creation of a Symlink File to a Directory in Linux

Using the ln command, you may also build a symlink to a directory. Use the link directory as the second parameter and the specified directory name as the first parameter to do this.

Here, we have to use the “-s” flag to create the symbolic link with the “ln” command. Then, we specified the path of the directory, which is “/home/saeed_raza/linux_folder1”. Next, we created the symlink to the directory “~/linux_folder2” from the “/home/saeed_raza/linux_folder1” directory. With the “ls” command and “-l” flag, we have shown the creation of the symlink to a specified directory.

Example # 4: Using the ln Command to Replace a Current Symlink in Linux

An existing symlink can be replaced using the ln command. The error will occur on the terminal if you attempt to build a symlink that has been created with the -s flag. To avoid exceptions on the terminal screen, you can manually replace an already-existing symbolic link by using the “-f” flag as seen below:

Here, we have the “ln” command using the “-sf” flag that helps us to create the existing symbolic link “link_file.txt”.

Example # 5: Removing the Link Files in Linux

By using unlink or rm commands, you can delete any existing links attached to files or folders. Using the unlink command, you can accomplish the following:

To the Ubuntu shell, we have given a command that has the “rm” command. The “rm” command takes the file name “link_file.txt” for removal. To verify whether the link file is removed or not, we have executed the “ls” command with the “-l” option and also provided the file name “link_file.txt”. You can see that the prompt generated the statement “No such file or directory”. It is an indication that the file is removed.

The rm command, which removes symbolic links, is comparable to the unlink command, which is as follows:

Like the “rm” command, we have utilized the unlink command to remove the file. We have given another file name “link_file2.txt” to the unlink command. Then, with the “ls” command, the file removal verification is done.

Note that with the “ln” command, new directory structures cannot be made. All subdirectories and directories indicated should already exist before executing the Linux “ln” command to create links; otherwise, errors may occur.

Conclusion

The “ln” command’s usage has been covered in this tutorial. The “ln” command is used to construct hard links and symlinks, as shown in the examples. In addition, we talked about using the “ln” command to connect files and directories.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.