Skip to content

[Linux] Use "ln" Command to Create Soft Link and Hard Link Files

Last Updated on 2024-08-07 by Clay

In Windows, we always use .lnk extension file (never show) redirect to a file path, it can help us to open any file easily.

Let me give an example that I think is more practical: today we have two hard disks in our computer, one is an SSD system disk and the other is a large capacity HDD as a storage device. Generally speaking, our desktop and system root directories will all by placed on SSD, so the files we are more convenient to open are placed on SSD.

But here comes the problem. Today I have a lot of music, hundreds of albums, and a size of more than 100 gigabytes. Where should I put them?

Put them on Desktop for me to easily open? But the capacity is more precious! and music files don't actually need to be accelerated in SSD.

But put them in HDD, maybe every time I want to play a song, I have to go around a long way to another hard drive to open it. Isn't it very inconvenient?

So today's protagonist ln command is long overdue. In Linux operating system, we can make a soft link to the folder, put the folder we want to open in the SSD, but link it to the HDD storage device. In this way, we can open it easily and enjoy the benefit of not consuming SDD capacity.

In addition to the above-mentioned functions, it is actually a very convenient management structure when writing program experiments.

The topic is far away, here is how to use the ln command.


Hard Link

The first thing to note is that in a Linux system, each file will have its own Inode. The so-called "hard link" is an pointer that points the hard link file we created to the original file, so that the system does not need to allocate an Inode for the newly created file.

For example, I have a file named LIFE (I really have it to put some life planning) and then I want to create its hard link file.

ln LIFE LIFE_2


In this way, I created a hard link file LIFE_2 of the LIFE file. We can use the following command to view their Inode:

ls -i LIFE LIFE_2

Output:

7515764 LIFE
7515764 LIFE_2

It can be found that their Inodes are exactly the same. This means that the two files are actually the same file, and the LIFE_2 actually does not take up space at all. And, when one of them is modified, the other will be modified together.

But if you delete one the them, the file will still exist. Only when all the files of this Inode are deleted, this file will really disappear in our storage device.

However, it should be noted that a hard link cannot link a folder.


Soft Link

Soft link, also known as symbolic link. basically I think the advantage is that it can link to folders.

Suppose I want to create a soft link to the "Music" folder under the "Enjoy" folder, and the body of my "Music" folder is always in the HDD, then I can use the following command:

ln -s /D/Music Enjoy/Music

In this way, the "Music" folder will automatically appear under my "Enjoy" folder, and it will point directly to the music folder in the D slot.


Moreover, we can use the following command to check the file size:

du Enjoy

Output:

4 Enjoy/

how is it? Will not occupy precious SSD space, right?


If you want to cancel the soft link, you can use the following command:

unlink Enjoy/Music

We will find that the soft-linked folder has completely disappeared.


References


Read More

Tags:

Leave a ReplyCancel reply

Exit mobile version