Linux command: ln

Quick startlnCommand to create a link in the file system

ThislnThis command is part of Linux file system commands.

It is used to create links. What is a link? It's like a pointer to another file. A file that points to another file. You may be familiar with Windows shortcuts. They are similar.

We have two types of links:Hard linkwithSoft link.

Hard links are rarely used. They have some limitations: you cannot link to a directory, nor can you link to an external file system (disk).

Use the following method to create a hard link

ln <original> <link>

For example, suppose you have a file named recipes.txt. You can use the following methods to create a hard link to it:

ln recipes.txt newrecipes.txt

The new hard link you create is no different from a regular file:

Now, whenever you edit any of these files, the content will be updated at the same time.

If you delete the original file, the link will still contain the original file content, because the content will only be deleted after a hard link points to the file.

Soft links are different. They have more powerful functions because you can link to other file systems and directories, but when the original file is deleted, the link will be broken.

you can use it-ss Choiceln:

ln -s <original> <link>

For example, suppose you have a file named recipes.txt. You can use the following methods to create a soft link to it:

ln -s recipes.txt newrecipes.txt

In this case, you can see that there is a speciallMark when you use list filesls -alAnd the file name has a@Finally, if color is enabled, its color will be different:

Now, if you delete the original file, the link will be broken, and if you try to access it, the shell will tell you "No such file or directory":

This command can be used in Linux, macOS, WSL and wherever you have a UNIX environment

Download mine for freeLinux Command Manual


More cli tutorials: