The command ln in Linux: create symbolic and hard links

Introduction

In the Linux world, everything is a file and understand how they relate to each other is fundamental to managing the system efficiently. One of the most useful concepts, but sometimes confused, is that of links. The commandlnallows to create both hard and symbolic links, each with its own characteristics and cases of use.

Hard links

A hard link is a direct reference to the i-node of a file. When you create a hard link, you're 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.

  • You cannot create hard links to directories (in most systems).
  • They don't work between different file systems or partitions.
  • Remove the original file does not delete the data while at least one hard link exists.

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 direct access: when opening the link, the system follows the route indicated and reaches the destination.

  • They can aim at files or directories, even if they are in different partitions.
  • If the target is removed or moved, the symbolic link is broken (dangling).
  • The size of the symbolic link corresponds to the length of the route it contains.

Basic command syntax ln

The commandlnhas two main forms depending on the type of link you want to create:

  • ln [origen] [enlace]→ creates a hard link.
  • ln -s [origen] [enlace]→ creates a symbolic link.

Where[origen]is the existing file or directory and[enlace]is the name you want to assign to the new link.

Practical examples

Suppose you have a file callednotas.txtin 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 running these commands, both new names will point to the same content. If you editnotas_hard.txtthe change will be seen innotas.txtandnotas_sym.txt. However, if you deletenotas.txt, the hard linknotas_hard.txtwill remain accessible because it still points to the i-node, while the symbolic linknotas_sym.txtIt'll be broken.

Use considerations

Choosing between a hard and a symbolic link depends on your needs:

  • Use hard links when you need multiple names for the same file and want to ensure that the data persists while there is at least one reference.
  • It prefers symbolic links when you want to create direct access that can cross file systems or when you need the link to reflect the name or location of the target (for example, to version libraries or scripts).

In addition, note that some programs may behave differently by following a symbolic link (for example, by usingreadlinkor when checking permits). Always test in a control environment before applying changes in production.

Conclusion

The commandlnis a simple but powerful tool that allows you to manage the way files are related in the Linux file system. 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 the guidelines presented, you are ready to uselneffectively in your daily administration tasks.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish