We will talk about how to create the symbolic links between the files in this article. We will also discuss some useful examples to understand the basic working of the ln command. All examples have been implemented on the Ubuntu 20.04 distribution.
The ln command syntax
Using the ln command, you can create the symlinks between files. This command creates the hard links on a file by default. However, using the (-s or –symbolic) option can also create symbolic links. The following is the syntax of the ln command that is given below:
The ln command creates a link from the specified file (file-name) to the second argument (link-name). However, no second argument is given or only (.) used as the second argument; then, it will create a link of the specified file into a current directory.
Two different kinds of links exist in the Linux system, soft or symbolic links and hard links.
Creating hard links to a file
A hard link can create one on more on a file. You cannot create the hard links for files and directories on a different partition or filesystem. The simplest way to create the hard links is using the ln command.
The above command creates a hard link with the name âlink_file.â
Creating a symbolic or soft link to a file
The symlink is an indirect file pointer. Unlike the hard links, The symbolic or symlink can point to a single file or directory on a different partition or filesystem. To create a symbolic or soft link, use the -s option along with the ln command as follows:
The above command creates the symbolic link with the name âlink_test_file.â
To show the created soft link, use the following ls command:
Create a symlink to a directory
You can also create a symlink to a directory through the ln command. For this purpose, use the directory name as the first argument, and the directory link will be used as the second argument.
For example, we are creating a symbolic link from the /home/kbuzdar/test-composer-project directory to the ~/my_project directory by using the following command:
Overwrite an existing symbolic link
Using the ln command, you can overwrite an existing symlink. For example, if you try to create a symlink that already created, then the following error will show on the terminal:
Using the â-fâ option, you can forcefully overwrite an existing symbolic link as follows:
The above will forcefully create the symlink that already exists.
Conclusion
We have discussed in this tutorial how to use the ln command. We have mentioned different examples of how to create symlinks using the ln command. Moreover, we have discussed how to create links between files and directories using the ln command. I hope the examples mentioned above will help you to understand the ln command. Explore more information about the ln command using the man page of the ln command.