Skip to main content

Understanding the `ln` Command in Linux: A Comprehensive Guide

The ln command in Linux is a standard utility that allows you to create links between files. These links can be either hard links or symbolic (soft) links, depending on how you use the command options. This article aims to offer a comprehensive overview of the ln command, its syntax, available options, and practical examples.

Syntax of ln Command

The basic syntax of the ln command is:

ln [OPTIONS] TARGET [LINK_NAME]
  • OPTIONS: Various flags and options that can modify the behavior of the ln command.
  • TARGET: The name of the file or directory that you want to link to.
  • LINK_NAME: The name that you want to assign to the link.

If LINK_NAME is omitted, ln will create a link with the same name as TARGET in the current working directory.

Table of Options

OptionShorthandDescription
--backup-bCreate a backup of existing destination files.
--directory-FTreat the link name as a directory, even if it is a symlink to a file.
--force-fRemove existing destination files to create the link.
--interactive-iPrompt before removing existing destination files.
--logical-LDereference TARGETs that are symbolic links.
--no-dereference-nDo not dereference symlink—if LINK_NAME exists and is a symlink, the new link will replace it.
--symbolic-sCreate a symbolic link instead of a hard link.
--verbose-vDisplay name of files before linking.

Examples of Using the ln Command

To create a hard link in Linux, you can use the ln command without any options for symbolic linking (i.e., without the -s option). The basic syntax for creating a hard link is as follows:

ln TARGET LINK_NAME

Here TARGET is the file you want to link to, and LINK_NAME is the name you want to give to the new hard link.

Example:

Let's say you have a file named file1.txt and you want to create a hard link to this file named hardlink1.txt. You would use the following command:

ln file1.txt hardlink1.txt

After running this command, file1.txt and hardlink1.txt will be two different names for the same content. If you modify one, the other will reflect those changes, because they point to the same inode and, therefore, the same data blocks on the disk.

You can verify that the hard link was created correctly by using the ls command with the -i option, which will display the inode number for each file:

ls -i file1.txt hardlink1.txt

If the inode numbers are identical, then you have successfully created a hard link.

Yes, a single file can have multiple hard links. In fact, that's one of the key features of hard links.

Example:

Suppose you already have file1.txt and its hard link hardlink1.txt. Now, you want to create another hard link to file1.txt, named hardlink2.txt.

You can do so using the following command:

ln file1.txt hardlink2.txt

Now, file1.txt, hardlink1.txt, and hardlink2.txt are all hard links pointing to the same inode, and consequently, to the same data blocks on the disk. You can verify this by checking their inode numbers:

ls -i file1.txt hardlink1.txt hardlink2.txt

All three should display the same inode number, confirming that they are indeed hard links of the same file.

To create a symbolic link (also known as a "soft link") in Linux, you use the ln command with the -s option. The basic syntax for creating a symbolic link is as follows:

ln -s TARGET LINK_NAME

Here, TARGET is the file you want to link to, and LINK_NAME is the name you want to give to the new symbolic link.

Example:

Let's say you have a file named file1.txt and you want to create a symbolic link to this file named symlink1.txt. You would use the following command:

ln -s file1.txt symlink1.txt

After running this command, symlink1.txt will be a symbolic link that points to file1.txt. If you modify the original file1.txt, the changes will be reflected when accessing it through symlink1.txt.

You can verify that the symbolic link was created correctly by using the ls command with the -l option:

ls -l symlink1.txt

The output should indicate that symlink1.txt is a symbolic link pointing to file1.txt.

Yes, a single file can have multiple symbolic links pointing to it, much like how a single file can have multiple hard links.

Example:

Suppose you already have file1.txt and a symbolic link symlink1.txt pointing to it. Now you want to create another symbolic link to file1.txt, named symlink2.txt.

You can do so using the following command:

ln -s file1.txt symlink2.txt

Now, file1.txt has two symbolic links pointing to it: symlink1.txt and symlink2.txt. You can check this by listing both symbolic links:

ls -l symlink1.txt symlink2.txt

Both should indicate that they are symbolic links pointing to file1.txt.

Creating multiple symbolic links to a single file is a straightforward task in Linux. This capability is particularly useful in scenarios where you want the same file to be accessible from different locations or under different names without duplicating the file's content. Keep in mind that, unlike hard links, symbolic links are just pointers, so if the original file is moved or deleted, all the symbolic links pointing to it will break.

Final Thoughts

The ln command is an incredibly versatile and useful utility in the Linux toolbox. While it may seem simple on the surface, its range of options allows for a wide array of linking strategies for both files and directories. By mastering the various options and their combinations, you can make your Linux file management more efficient and tailored to your specific needs.

TypeScript Course Instructor Image
TypeScript Course Instructor Image

Time To Transition From JavaScript To TypeScript

Level Up Your TypeScript And Object Oriented Programming Skills. The only complete TypeScript course on the marketplace you building TypeScript apps like a PRO.

SEE COURSE DETAILS

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for ssoftware developers.

YouTube @cloudaffle