How to Use the ln Command in Linux – 5 Examples

ln-command-in-linux

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”

We’ve added the -v for verbose of the performed action.

ln-command-create-hard-link

We can run the Linux ls command to verify that our hard link has been created.

verify-created-hard-link

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.

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.

ln-command-in-linux-create-soft-link

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.

hard-link-vs-soft-link

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.

Here’s an example.

ln-command-in-link-soft-link-directory

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.

verify-directory-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.

ln-cmmand-error-overwriting-a-link

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.

ln-command-overwrite-link

5. Unlinking Created Links

You can delete the created links using the rm command or the unlink option. Use either of the below syntax.

The process is the same whether unlinking a hard or soft link.

unlink-a-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.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top