Introduction
In the Linux world, everything is a file and understanding how they relate to each other is essential for administering the system efficiently. One of the most useful, yet sometimes confused, concepts is that of links. The ln command allows you to create both hard and symbolic links, each with its own characteristics and use cases.
Hard Links
A hard link is a direct reference to a file’s i‑node. When you create a hard link, you are creating another name that points exactly to the same data block on disk. Therefore, any change made through one of the names is instantly reflected in the others.
- Hard links cannot be created to directories (on most systems).
- They do not work across different filesystems or partitions.
- Deleting the original file does not erase the data as long as at least one hard link remains.
Symbolic Links (soft links)
A symbolic link, also called a soft link, is a special file that contains a path to another file or directory. It acts as a shortcut: when you open the link, the system follows the indicated path and reaches the target.
- They can point to files or directories, even if they are on different partitions.
- If the target is deleted or moved, the symbolic link becomes broken (dangling).
- The size of the symbolic link corresponds to the length of the path it contains.
Basic syntax of the ln command
The ln command has two main forms depending on the type of link you want to create:
ln [source] [link]→ creates a hard link.ln -s [source] [link]→ creates a symbolic link.
Where [source] is the existing file or directory and [link] is the name you wish to assign to the new link.
Practical examples
Suppose you have a file called notas.txt in your home directory.
- To create a hard link:
ln notas.txt notas_hard.txt
- To create a symbolic link:
ln -s notas.txt notas_sym.txt
After executing these commands, both new names will point to the same content. If you edit notas_hard.txt, the change will be seen in notas.txt and in notas_sym.txt. However, if you delete notas.txt, the hard link notas_hard.txt will remain accessible because it still points to the i‑node, while the symbolic link notas_sym.txt will become broken.
Usage considerations
Choosing between a hard link and a symbolic link depends on your needs:
- Use hard links when you need multiple names for the same file and want to guarantee that the data persists as long as at least one reference exists.
- Prefer symbolic links when you want to create shortcuts that can cross filesystems or when you need the link to reflect the name or location of the target (for example, to version libraries or scripts).
Also, keep in mind that some programs may behave differently when following a symbolic link (for example, when using readlink or when checking permissions). Always test in a controlled environment before applying changes in production.
Conclusion
The ln command is a simple yet powerful tool that allows you to manage how files relate to each other in the Linux filesystem. Understanding the difference between hard and symbolic links will help you avoid problems such as infinite loops, broken files, or unnecessary space consumption. With the examples and guidelines presented, you are now ready to use ln effectively in your daily administration tasks.
This post is also available in ESPAÑOL.