The ln command in Linux creates links. A link lets you have different names for the same file stored in different locations. You can create two types of links: hard and soft links.
The hard links bind two or more file names to the same inode and can only be used for files and directories in the same filesystem. However, soft links are symbolic links created as references to another file or directory. Soft links are like “shortcuts” to quickly access frequently used files or directories stored anywhere in the filesystem or partition.
This post focuses on using the “ln” command in Linux to create links.
ln Command in Linux with Examples
The ln command lets you create a hard or soft link depending on the options that you pass to it. By default, the ln command creates hard links. However, adding the -s option specifies that you want to create a soft link.
Below are examples of creating links using the ln command in Linux.
1. Creating Hard Links
As mentioned earlier, the default behavior of the ln command is to create a hard link. To do so, simply run the ln command without the -s option and provide the target file and the new name of the hard link.
The example below creates a hard link for the “hosts.txt.” We’ve named the hard link “hard_link_hosts.txt”
$ ln [option] target_file hard_link_name
We’ve added the -v for verbose of the performed action.
We can run the Linux ls command to verify that our hard link has been created.
2. Creating a Soft Link
A soft link is referenced as a symbolic link or symlink. Additionally, it acts as a pointer to another directory or file. To create a soft link, run the ln command with the -s option.
$ ln -s [option] target_file hard_link_name
Using the same file for which we’ve created a hard link, let’s create a symlink and see the difference. We will run our command as follows.
When we run the ls command to verify that our soft link has been created, we get a different output from the hard link. First, the soft link’s color is different. Moreover, it has a pointer to the actual file, confirming that it is merely a shortcut.
Note the difference in the output below between the soft and hard links.
3. ln Command in Linux Create Soft Link for a Directory
You can also create a symbolic link for a directory, similar to what we’ve done with the file. The created soft link will be a shortcut for the original directory and can be created using the below syntax.
$ ln -s [option] target_directory hard_link_name
Here’s an example.
When we verify that the soft link has been created, we also get a highlighted output pointing to the original directory for which we’ve created a soft link.
4. Overwrite an Existing Link
Updating an existing link is possible when using the ln command in Linux. However, if you try updating it, you will run into an error like the one below.
To bypass the error, you can use the -i option to overwrite the link interactively or use the -f option to update it forcefully.
We’ve used the -f option for this example and are updating a soft link to a directory.
5. Unlinking Created Links
You can delete the created links using the rm command or the unlink option. Use either of the below syntax.
$ unlink link_name or $ rm link_name
The process is the same whether unlinking a hard or soft link.
Conclusion
The “n command in Linux creates hard and soft links. To create a soft link, add the -s option. Otherwise, a hard link will be created by default. We’ve discussed hard and soft links and given different examples of creating them. Lastly, we’ve seen how to unlink/remove the created links.