Linux command line, file links: the ln command
Greetings!

This is the fifth post in the series on the Linux command line. Today we’ll talk about file links (hard and symbolic), and also look at the command for creating them: ln.

Previous posts in the series

If you’re just starting to learn Linux, I strongly recommend checking out the previous posts:

The practical examples in this series of posts were performed in a Linux Mint 21 distribution environment.

Introduction

Link files, also known as hard links and symbolic links, are a core part of many operating systems, including Linux. Links allow a user to create links to files and directories and use them as alternative access paths to data.

The linking mechanism was introduced in Unix in the early 1970s, and since then it has become an important part of Unix-like operating systems.

To work with links in Linux there’s the ln command. With this command you can create both hard and symbolic links.

To create hard links it’s enough to use the ln command without keys. To create symbolic links, the -s key is required. Symbolic links are more commonly used.

Let’s move on to the ln command.

Syntax:

BASH
ln [keys] <path_to_target_file> <path_to_link_file>
Click to expand and view more

Frequently used keys:

KeyValue
-voutput of the actions being performed
-iinteractive mode (asks before overwriting the target file with the link file)
-screate a symbolic link to a file
-fignore warnings about existing files

For help use the commands:

BASH
ln --help

man ln
Click to expand and view more

Symbolic links in Linux are often compared to shortcuts in Windows. They’re similar, but they differ somewhat in their working mechanism.

To create a symbolic link, as already mentioned, you need to pass the -s (symbolic) key to the ln command, then specify the path to the file the link will point to, and then the path to the link itself.

The -v -i -f keys of the ln command work the same way as with the cp and mv commands, which we got acquainted with last time.

! Note that the path to the file the link points to is a fundamentally important parameter — it can be absolute or relative, and this matters. We talked about absolute and relative paths here.

Now let’s practice.

Let’s create test directories and simple test files. If you don’t know how this is done, we covered working with files in detail in the previous post.

Let’s create two directories: testdir and, inside it, testdir2, as well as one empty file new_text_file.txt, which we’ll fill with any content using the graphical editor xed (I added the line “Bye R4ven.me!!!”):

BASH
mkdir -p testdir/testdir2

cd testdir

touch new_text_file.txt

xed new_text_file.txt
Click to expand and view more

So, we’re in the ~/testdir directory, let’s create a symbolic link to our file ~/testdir/new_text_file.txt in the testdir2 subdirectory:

BASH
ln -sv ~/testdir/new_text_file.txt testdir2/

ls -l testdir2/
Click to expand and view more

Note that in the ls command output, the link file is highlighted in a different color, and in the ls -l output, the first character in the link’s description denoting the file type is l, which stands for symbolic link. Also, with this output you can see what this link points to. When accessing this link, it will redirect us to another file. Let’s try outputting the content of the link file:

BASH
cat testdir2/new_text_file.txt

cat new_text_file.txt
Click to expand and view more

As we can see in the cat command output, the content is identical. Let’s create a symbolic link to this file with a relative path:

BASH
ln -sv ../new_text_file.txt ./testdir2/
Click to expand and view more

We’ll see an error, since a file with that name already exists. To overwrite it we use the -f key:

BASH
ln -svf ../new_text_file.txt ./testdir2/

ls -l testdir2/
Click to expand and view more

We see that the link’s address has changed.

Symbolic links can be created for directories and any other Linux files as well. Let’s create a link to our home directory and go to that path:

BASH
ln -sv ~/ ./link_to_home

ls -l

cd link_to_home

pwd
Click to expand and view more

As we can see, we ended up “inside” the link, this is the so-called logical path. This is shown by the output of the pwd command and the address in our shell’s prompt (more about prompt). But in fact, we simply went into our home directory.

You can verify this with the pwd command using the -P (physical) key, which outputs our physical location:

BASH
pwd -P
Click to expand and view more

The linking mechanism is very convenient and is often used to maintain compatibility, if system files or program files have changed their location in newer versions, or if the name/version of some program has changed.

For interest, run the ls -l command in the /usr/bin directory, where executable files are stored, and see how many symbolic links there are and where they lead:

BASH
ls -l /usr/bin
Click to expand and view more

Hard links are links to files that point to the same index record (inode) on the file system as the original file. A hard link and the original file can be used as alternative access paths to the same file, and for any application or user that uses either of these paths, the file will look and work the same.

One of the main differences between hard links and symbolic links is that hard links can only be created for files, while symbolic links can be created for both files and directories. Additionally, hard links cannot be created for files located outside a single file system/disk partition, whereas symbolic links can.

Let’s look at a few examples. Being in the testdir directory, let’s copy the executable file of the ls command into it and create a hard link to it with a different name:

BASH
cd ~/testdir

cp /usr/bin/ls .

ln ./ls hard_ls

ls -l
Click to expand and view more

As we can see, we now have two executable files: ls and hard_ls. In fact, this is the same file, which takes up one disk space. And these files ls and hard_ls are just links to this space.

Here are a few features of hard links in Linux:

  1. Hard links to the same file have the same inode number.
  2. Hard links don’t have their own data block in the file system, they are simply other names for the file.
  3. Deleting a hard link doesn’t affect the original file, as long as there’s still another hard link pointing to it. Only when all links to the file are deleted is it actually deleted.

To view a file’s inode (disk descriptor) you can use the ls -i command. Let’s check:

BASH
ls -li
Click to expand and view more

The inode number is shown in the first column of the ls command output. As you noticed, our file ls and its hardlink have the same disk descriptor, i.e. inode number. This means both of these files point to the same data block on the hard disk.

Also pay attention to column №3 of this output: here the number of hard links of the file existing in the system is shown.

We covered the ls command output in detail in the post about the ls command.

Let’s create one more hardlink to our ls file, only in a different place, for example in the Documents folder of our home directory:

BASH
ls -li

ln -v ./ls ~/Документы

ls -li ~/Документы
Click to expand and view more

As we can see, after creating another hard link, the number in the file’s attributes increased, while the inode number is the same for all files.

The ls command isn’t the only way to find out a file’s inode number. For this you can use the useful stat command, which outputs a file’s metadata. Let’s try:

BASH
stat hard_ls
Click to expand and view more

We see the number, the link count and other useful info.

Afterword

Today we looked at an important and interesting mechanism of file links in Linux. We learned how hard links differ from symbolic links and learned how to create and use them with the ln command. In the next post we’ll look at another very useful and powerful mechanism: input, output and error output redirection in the Linux console.

Thanks for reading. Good luck becoming a “linuxoid.”

Useful sources

Copyright Notice

Author: Ivan Cherniy

Link: https://r4ven.me/en/linux/komandnaya-stroka-linux-ssylki-na-fajly-linki-komanda-ln/

License: CC BY-NC-SA 4.0

Blog materials may be used with attribution to the author and source, for non-commercial purposes, and under the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut