link Command in Linux



link is a command used in Linux to create hard links to files. A hard link acts as an alternative name for an existing file, directly pointing to its data on the disk. So, if you delete the original file, the hard link continues to hold onto the data. It is because it points to the same location on the disk. The link is a pretty straightforward command that only creates hard links, unlike ln command which can create both hard and symbolic links.

Table of Contents

Here is a comprehensive guide to the options available with the link command −

Syntax of link Command in Linux

The basic syntax to use the Linux link command is provided below −

link TARGET LINK_NAME

Where,

  • TARGET specifies the path of the existing file for which you intend to create a hard link.
  • LINK_NAME is the name of the new hard link you are creating.

link Command Options

Since the link command is used solely for creation, there are no specific options available for use, except --help and --version options.

How to Use link Command in Linux?

The fundamental use of the link command on Linux is to create a link that points directly to the target file. This establishes a hard link, effectively creating another directory entry for the file without duplicating the actual file content. For example, if you have a file named file1.txt and you want to create a hard link named file1_link.txt, you can use −

link file1.txt file1_link.txt
link Command in Linux1

This command creates a new directory entry for file1.txt named file1_link.txt, without duplicating the actual file content. Both file1.txt and file1_link.txt will point to the same inode, meaning changes to one will reflect in the other.

To check whether a link is successfully created, you can simply use the stat command followed by the file name that is used to create the link. The stat command provides you with a detailed information about the file, including its type, size, and the number of hard links. For example −

stat file1.txt
link Command in Linux2

The Links: 2 indicates there are two hard links to this file.

Conclusion

The link command is a vital utility in Linux for creating hard links to files, providing an additional name that points directly to the file’s data on disk. This means that the hard link retains the data even if the original file is deleted, as they both point to the same disk location.

While link is limited to creating hard links, the ln command is more versatile, supporting both hard and symbolic links. Mastering these commands is crucial for effective file management in Linux.

Advertisements