fbpx ln Command in Linux: Creating Symbolic and Hard Links | Certificação Linux

ln Command in Linux: Creating Symbolic and Hard Links Between Files

📅 01/01/2019 ⏱ 3 min ✍️ Uira Ribeiro
Linuxcommands
ln Command in Linux: Creating Symbolic and Hard Links Between Files
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

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

The most commonly used options for the ln command are:

  • -s Creates a symbolic link. The default for the ln command is 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 different filesystems.

Hard links, on the other hand, can only be created between files on the same filesystem on the same physical volume (disk).

Examples:

Note the file quintana, it has a hard link count equal to 1:

$ ls -l-rw-rw-r--. 1 uira uira 177 Set 18 15:09 quintana

Let's create a hard link between file quintana with the name poem:

$ ln quintana poema

If we check with ls -l, we will see that the hard link count for these files changed from 1 to 2.

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

$ ls -l-rw-rw-r--. 2 uira uira 177 Set 18 15:09 poema-rw-rw-r--. 2 uira uira 177 Set 18 15:09 quintana

In this way, it is as if the same set of bytes on disk had two names, one called poem and another called quintana. The content of both is the same, and if we modify the content of one, the other will also be modified, since the file's set of bytes 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 uira uira 177 Set 18 15:09 poema

Now let's create a symbolic link called poetry from the poem file:

$ ln -s poema poesia$ ls -l-rw-rw-r--. 1 uira uira 177 Set 18 15:09 poemalrwxrwxrwx. 1 uira uira   5 Set 18 15:20 poesia -> poema

Note that a symbolic link creates a pointer to the original file. It will always have the permissions "lrwxrwxrwx", 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 modify the symbolic link as if you were modifying the file itself, without any problem.

If you delete the original file, the link will become broken and will stop working. Usually the system indicates this by coloring the original file with a black background and the link will blink.

$ rm poema 
$ ls -llrwxrwxrwx. 1 uira uira 5 Set 18 15:20 poesia -> poema

Symbolic links greatly help system administration, since they allow "shortcuts" to files to be created without the direct need to make copies.

Using a hard link allows multiple filenames to be associated with the same file, since a hard link points to the inode of a specific file. It is like making a copy, but without actually duplicating disk space.

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

Liked it? Share

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp