Overview
The `link` command creates a hard link using two specified file paths. The first argument is the path to the existing file, and the second argument is the path for the newly created hard link. This command is similar to the hard link creation functionality of the `ln` command, but it exclusively provides hard link creation without additional options.
Key Features
- Creates a hard link to an existing file
- Shares the same inode as the original file
- Works only within the same file system
- Cannot create hard links to directories
Usage Examples
Here are a few examples of using the `link` command.
Creating a basic hard link
echo "Hello, World!" > original.txt
link original.txt hardlink.txt
ls -li original.txt hardlink.txt
Creates a hard link named `hardlink.txt` to the file `original.txt`.
Verifying access after deleting the original file
echo "This is a test." > file_to_link.txt
link file_to_link.txt linked_file.txt
rm file_to_link.txt
cat linked_file.txt
Demonstrates that data can still be accessed through the hard link even after the original file is deleted.
Tips & Considerations
Useful tips and points to consider when using the `link` command.
Understanding Hard Links
- Inode Sharing: Hard links have the same inode number as the original file, meaning both names point to the same actual data. The file system tracks the inode's link count, and the actual data is not deleted until this count reaches zero.
- File System Boundaries: Hard links can only be created within the same file system. You cannot create a hard link to a file on a different partition or mounted disk. In such cases, you should use a symbolic link (symlink).
- Cannot Link Directories: The
linkcommand cannot create hard links to directories. Directory hard links are generally not allowed as they can compromise file system integrity.
Difference between link and ln
- Functionality: The
linkcommand exclusively creates hard links. In contrast, thelncommand can create symbolic links using the-soption and offers more options such as-f(force overwrite) and-v(verbose output). - Ease of Use: In most cases, the
lncommand is more flexible and convenient, making it widely used.linkis a more basic utility that only provides the hard link creation functionality ofln.