Linux Command ln: Symbolic and Hard Links (4 Key Options)

The Linux command ln is used to create links, an essential feature in the file system. A link provides another name for a file within the file system, and there are two main types: hard links and symbolic links. In this post, we will explore the basic usage of the ln command, its various options, and how to efficiently utilize links.

A hard link allows multiple names to access the same file. All hard links share the same inode number and directly point to the file’s data blocks. The file content is singular but appears as multiple files in different locations.

Characteristics of hard links:

  • Can only be created within the same file system.
  • The file can still be accessed through other hard links even if the original file is deleted.
  • Hard links cannot be used with directories.

A symbolic link is a special file that points to the location of another file, similar to a shortcut. It stores the path of the original file, and changes to or deletion of the original file affect the symbolic link.

Characteristics of symbolic links:

  • Can be created across different file systems.
  • If the original file is deleted, the link becomes a broken link.
  • Can be used with directories.

Usage of the Linux Command ln

The ln command in Linux is used to create links. The basic syntax is as follows:

ln [options] original_file link_name
ShellScript

To create a hard link, simply use the ln command. This command creates a hard link named hard_link for the original_file.

ln original_file hard_link
ShellScript

After creating a hard link, using the ls -l command shows that the link appears just like a regular file.

Figure 1. Linux Command ln: Creating a Hard Link
Figure 1. Linux Command ln: Creating a Hard Link

Next, we’ll verify hard_link and original_file using the stat command. Both files have the same inode number, 1311921, and the Links value is 2, indicating two files share the same inode.

Figure 2. Verifying Hard Link with the Linux Command stat
Figure 2. Verifying Hard Link with the Linux Command stat

If we modify hard_link and then check the contents of original_file, both files will reflect the same changes because they share the same inode.

Figure 3. Updating a Hard Link File Changes the Original File
Figure 3. Updating a Hard Link File Changes the Original File

Deleting original_file leaves hard_link as an independent file. The inode remains the same, but the Links value changes to 1, showing there are no more hard links.

Figure 4. Verifying the Remaining File with the Linux Command stat
Figure 4. Verifying the Remaining File with the Linux Command stat

To create a symbolic link, use the -s option. This command creates a symbolic link named symbolic_link for the original_file.

ln -s original_file symbolic_link
ShellScript

When a symbolic link is created, the ls command displays an l at the beginning of the file attributes, indicating a link. Terminal colors may vary to show the link, and the format “symbolic_link -> original_file” makes it clear which file the link points to.

Figure 5. Linux Command ln: Creating a Symbolic Link
Figure 5. Linux Command ln: Creating a Symbolic Link

The stat command reveals what the symbolic link points to and clearly indicates it is a symbolic link.

Figure 6. Verifying a Symbolic Link with the Linux Command stat
Figure 6. Verifying a Symbolic Link with the Linux Command stat

Key Options

-f (force)

The -f option forcibly overwrites existing files or links.

ln -f original_file link_name
ShellScript

If a file with the same name already exists, attempting to create a link without this option results in an error message “ln: failed to create hard/symbolic link ‘filename’: File exists” indicating the file already exists.

Figure 7. Forcibly Overwriting with the Linux Command ln -f Option
Figure 7. Forcibly Overwriting with the Linux Command ln -f Option

-i (interactive)

The -i option prompts the user for confirmation before overwriting a file.

ln -i original_file link_name
ShellScript

When a file already exists, the -i option asks “ln: replace ‘link_name’?” before creating the link.

Figure 8. Prompting Before Overwriting with the Linux Command ln -i Option
Figure 8. Prompting Before Overwriting with the Linux Command ln -i Option

-v (verbose)

The -v option provides detailed output of the command execution.

ln -sv original_file link_name
ShellScript

When creating a hard link, it uses => to indicate the link creation, and -> for symbolic links.

Figure 9. Verifying Link Types with the Linux Command ln -v Option
Figure 9. Verifying Link Types with the Linux Command ln -v Option

Useful Applications

Symbolic links can be used with directories. For example, you can link frequently used configuration files or directories for easy access.

ln -s /path/to/original_directory /path/to/link_directory
ShellScript

Version Management

Symbolic links are useful for managing multiple versions of files. In software development, you can link a specific version of a file to current to always access the latest version.

ln -s version1.8 current
ShellScript

System Configuration Files

Symbolic links simplify the management of system configuration files. For instance, you can use a symbolic link to maintain the same .bashrc file across different environments.

ln -s /path/to/shared_bashrc ~/.bashrc
ShellScript

Precautions

When using the ln command, there are a few precautions to keep in mind. Make sure to remember these to avoid issues.

Hard links can only be created within the same file system. If you try to create a hard link across different file systems, you will encounter an error message. In such cases, use the -s option to create a symbolic link.

Figure 10. Linux Command ln: Hard Links Cannot Cross File Systems
Figure 10. Linux Command ln: Hard Links Cannot Cross File Systems

Symbolic links store the path to the original file. If the original file is moved or deleted, the link will break. Therefore, when moving or deleting the original file, update or remove the corresponding symbolic link.

If the original file is deleted, ls -l will show the broken link in red.

Figure 11. Broken Symbolic Link: Original File Deleted
Figure 11. Broken Symbolic Link: Original File Deleted

Use appropriate relative or absolute paths when creating symbolic links. If you move the symbolic link file, relative paths can cause the link to break because they are based on the link’s location.

If a symbolic link is created with a relative path and then moved, you need to update the original file’s path or recreate the symbolic link. Alternatively, use an absolute path to avoid this issue.

In this example, sym_link is a relative path symbolic link from the ~/linux/ln directory to b/original_file. Moving sym_link breaks the link due to the changed relative path.

Figure 12. Broken Symbolic Link: Relative Path Used and Link Moved
Figure 12. Broken Symbolic Link: Relative Path Used and Link Moved

Permission Issues

Link creation may fail due to file system permissions. If the original file is owned by root, you may encounter an error message. Use sudo to create the link with administrative privileges, ensuring the new link has appropriate ownership.

Figure 13. Using the Linux Command ln with sudo Due to Permission Issues
Figure 13. Using the Linux Command ln with sudo Due to Permission Issues

Summary

Using the Linux command ln allows you to efficiently manage files and directories. Understanding the differences between hard links and symbolic links and using the appropriate link type for the situation makes system management much easier.

By leveraging links for file management, version control, and configuration file management, you can make your Linux environment more flexible and efficient. Familiarize yourself with the various options of the Linux command ln and keep the precautions in mind to avoid mistakes.

Use hard and symbolic links to become a more proficient Linux user.

References

Leave a Comment