ln command on Linux (creates links) [Basic Guide]

The ln command in Linux can be used to create symbolic or physical links.

The most commonly used options for the ln command are:

  • -s Creates a symbolic link. The ln command defaults to hard links.
  • -f Forces the creation of a link even if it already exists.

Symbolic links can be created between files, or between directories, even on different disks and file systems.

Hard

links, on the other hand, can only be created between files from the same file system on the same physical volume (disk).

Examples:

Look at the Quintana file, it has a hard link count equal to 1:

$ ls -l 
-rw-rw-r--. 1 uira uira 17 Sep 18 15:09 Quintana

Let’s create the physical link between the Quintana archive with the name poem:

$ ln quintana poem

If we look with ls -l, we’ll see that the hard link counter for these files went from 1 to 2.

Link counting is the way to tell if a file has a hard link.

$ ls -l 
-rw-rw-r--. 2 uira uira 17 Sep 18 15:09 poem
-rw-rw-r-. 2 uira uira 17 Sep 18 15:09 quintana

In this way, it is as if the same set of bytes on the disk had two names, one called a poem and the other called Quintana. The content of the two is the same, and if we change the content of one, the other will also change, since the byte set of the file is the same.

Note that both files have the same permissions, same owners, same size, and same time.

If the Quintana file is deleted, the poem file remains, and no data is lost:

$ rm quintana $ ls -l -rw-rw-r--. 1 hour 17 Sep 18 15:09 poem

Let’s now create a symbolic link called poetry from the poem file:

$ ln -s poem poetry $ ls -l -rw-rw-r--. 1 uira uira 177 Sep 18 15:09 poem lrwxrwxrwx. 1 uira uira 5 Sep 18 15:20 poetry -> poem

Notice that the symbolic link creates a pointer to the original file. It will always have the “lrwxrwxrwx” permissions, and its size will always be small (5 bytes).

The arrow “->” and the file type “l” indicate that the file is a symbolic link.

You can edit and change the symbolic link as if you were altering the file itself, without any problem.

If you delete the original file, the link will be broken and will stop working. Generally, the system indicates coloring the original file with a black background and the link blinks.

$ in a poem
$ ls -l lrwxrwxrwx. 1 uira uira 5 Sep 18 15:20 poetry -> poem

Symbolic links help a lot with system administration, since they allow “shortcuts” to files to be created without the direct need to make copies.

The use of a hard link allows several file names to be associated with the same file, since a hard link points to the inode of a particular file. It’s like making a copy, but without actually doubling the disk space.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?