ln Command in Linux | Explained With Examples

In Linux, Linking one file or directory to the other file/directory is performed using the “ln” command. We can create hard and soft/symbolic links with the help of the “ln” command. A hard link is the link of a physical file that is taking the memory of the system, and a symbolic/soft link is only the hyperlinks of other files which redirect to that file.

The main advantage of the soft link is that it is linked to a file from another system that you can access without using your hard disk space, which is impossible in hard links. While in hard links original file can be accessed even if the file is deleted from the system.

This guide is an extensive tutorial on the ln command in Linux with the following outcomes:

Let’s start the guide!

What is the ln Command in Linux?

To create a link between the files, the ln is used. Normally, the “ln” command creates hard links to the files. However, the variety of options supported by the “ln” command allows the creation of symbolic links. Before getting into details, let’s have a look at the syntax of the ln command:

Syntax

$ ln [options]  <source> <destination>

The source shows the file whose link is to be created, and the destination indicates the file which will be linked to the source file.

Flags for ln Command:

  • b  Creates a backup of each existing file
  • f   Force the deletion of the existing files & overwrites it
  • i   Take permission to delete the destination
  • L  Submits the symbolic link targets
  • n  Behave symbolic link to the directory as a normal file
  • r   Creates a relative link to the link location
  • s  Crete symbolic (soft) link instead of Hard link
  • t   Set the Target directory for creating the links

 Let’s discuss the uses of the “ln” command.

How to Use ln Command in Linux?

The “ln” command creates a hard and soft link for a file. This section provides a detailed usage of the ln command to create the links to the files. Let’s dig into them:

Example 1: How to Create Hard Links Using ln Command?

The ln command creates a hard link. To create a hard link to a file named “file2.txt” to “newfile2.txt”, utilize the following command:

$ ln file2.txt newfile2.txt

To verify the created links, run the following command in the terminal:

$ ls -l newfile2.txt

Example 2 How to Create Symbolic Links Using ln Command?

Symbolic/Soft links are created with the “ln” command alongside its flag “s”. To create a file named “newfilw1.txt”, which is the symbolic link of the “file1.txt”, use the below0mentioned command:

$ ln -s file1.txt newfile1.txt
$ ls -l newfile1.txt

We can verify the soft link files by its “GUI”, as it has an “arrow” symbol at the bottom of the file as shown below:

Example 3: How to Force the Deletion of Existing Files & Overwrites?

If you want to create a “soft link” to a file that already “exists”, it will show the below error.

$ ln -s file2.txt newfile2.txt

To delete the existing soft “link” file & overwrite it with the same or another file, you need to use the force “-f” flag of ln command as shown below:

$ ln -sf file2.txt newfile2.txt

Example 4: How to Create a Symbolic link to Other Directory?

A directory can also refer to another directory, and such a scenario can be created using the “ln” command. For instance, to create a soft link of the “Music” directory in the “NewFolder1” directory, execute the below-written command:

Note: ~” refers to the home directory (/home/itslinuxfoss).

$ ln -s ~/Music /home/itslinuxfoss/NewFolder1

Example 5: How to Remove or Delete the Symbolic Links?

If the symbolic link of the file becomes unavailable or the link is broken, you need to delete/unlink the linked file. To unlink or delete a linked file, use the “rm” or “unlink” commands.

For instance, to delete or remove a soft link file named “newfile2.txt”, execute the below-written command in the terminal:

$ rm newfile2.txt

To unlink a linked file named “newfile2.txt”, use the following command:

$ unlink newfile2.txt

That’s the end of this informative guide!

Conclusion

The “ln” command in Linux creates soft and hard links for the files. To create a hard link, use the “ln [options]  <source> <destination>” command, and for creating a soft/symbolic link, execute the “ln -s[options]  <source> <destination>” command. We can also overwrite the existing linking file using the “f” flag of the “ln” command. This post has illustrated the extensive usage of the ln command in Linux.