Master the Linux ‘ln' Command: A Comprehensive Guide

Peter Hou
3 min readApr 19, 2023

This article provides a comprehensive guide to the Linux ln command, including its history, when and why to use it, how to use it, common parameters, tricky skills, and things to be aware of. The ln command is useful for creating links between files, which can help in organizing files, saving disk space, and simplifying file management. Understanding the difference between hard and symbolic links, their use cases, and various parameters will help you make the most of this command.

Instructions

In this article, we will cover the Linux ln command, its history, when and why to use it, how to use it, common parameters, some tricky skills, and things to be aware of while using this command.

History

The ln command, short for "link," was introduced in Unix operating systems. It is used to create links between files, allowing users to create either hard links or symbolic links.

When and why to use it

The ln command is useful for creating links between files, which can help in organizing files, saving disk space, and simplifying file management. Hard links provide a direct connection between two files, while symbolic links create a pointer to the original file.

How to use it

To create a link between two files, use the ln command followed by the source file and the target file.

ln source_file target_file

The commonly used parameters

  • -s: Create a symbolic link instead of a hard link.
ln -s source_file symbolic_link
  • -f: Force the creation of a link, even if the target file already exists.
ln -sf source_file target_file
  • -n: Treat the target file as a normal file, even if it is a symbolic link to a directory.
ln -n source_file target_file
  • -v: Display verbose information about the links being created.
ln -v source_file target_file
  • -i: Prompt for confirmation before removing an existing target file.
ln -i source_file target_file

Other supported parameters

  • -b: Create a backup of the existing target file before creating a new link.
  • -S: Specify a custom suffix for the backup file.
  • -T: Treat the target file as a regular file, even if it is a directory.

Most common use cases

The ln command is commonly used for:

  1. Organizing files by creating links to them in different directories.
  2. Saving disk space by linking to a single file from multiple locations.
  3. Simplifying file management by using links to reference the same file.
ln file1 link_to_file1

The tricky skills

  • Use readlink to display the target of a symbolic link:
readlink -f symbolic_link
  • Combine ln with find to create links for multiple files:
find /path/to/source -type f -exec ln -s {} /path/to/target/ \\;

What needs to be noted

  • Be aware of the difference between hard links and symbolic links.
  • Remember that hard links cannot span across different file systems or link to directories.
  • Deleting the original file does not affect hard links, but symbolic links will become invalid.

Conclusion

The Linux ln command is a versatile tool for managing files through links. Understanding the difference between hard and symbolic links, their use cases, and various parameters will help you make the most of this command.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet