Home  >  Article  >  Operation and Maintenance  >  Introduction to Linux file links and their applications

Introduction to Linux file links and their applications

齐天大圣
齐天大圣Original
2020-06-20 11:13:492318browse

There are two types of links to files in Linux, one is a hard link and the other is a soft link. Today I will introduce these two links to you and how to use them.

Hard links

First let’s look at hard links. Let’s compare them using variable references in PHP.

$a = 100;
$b = &$a;

You can think of $a as a file, and $b as a hard link to the $a file. The current effect is that whether $a or $b is modified, it will affect the other party.

Now if one of the files is deleted, will the other file still exist?

unset($b);

Even if the $b file is deleted, the $a file still exists.

Next, let’s see if it is the same as what was said above. The command to create a hard link is as follows:

ln 源文件 链接文件

The system currently has a 1.php file, now let’s create a hard link for it.

# ln 1.php 2.php

# cat 1.php

Now let’s modify the 2.php file to see if the 1.php file will also be modified.

# echo -e '\n?>' >> 2.php

# cat 1.php

You can see that if 2.php is modified, the 1.php file will also be modified. Now let's delete the 2.php file and see if the 1.php file still exists.

# rm -f 2.php
# ll 1.php
-rw-r--r-- 1 root root 52 Jun 20 08:05 1.php

You can see that even if 2.php is deleted, the 1.php file still exists.

The function of hard links is introduced above. Here are the restrictions on the use of hard links:

  • Cannot link directories

  • Cannot make links across file systems

Due to the limitations of the above two points, there are very few opportunities to use hard links in daily life. Here is another kind of link-soft link, which There are no restrictions as mentioned above, so I use it a lot! .

Soft link

Generally, soft links are compared with desktop shortcuts in Windows systems. By creating soft links, you can easily execute a command without Find the directory location of the file. In addition, the soft link file is deleted, but the source file still exists. However, if the source file is deleted, the linked file has no effect.

Soft links are divided into two types: files and directories. The functions of these two links are introduced below.

Make soft links to files

Have you ever thought about why you don’t need to use absolute paths after you install mysql? Use mysql to execute. Soft links are used here.

# which mysql
/usr/bin/mysql
# ll /usr/bin/mysql
lrwxrwxrwx 1 root root 26 Nov  4  2019 /usr/bin/mysql -> /usr/local/mysql/bin/mysql

It can be seen that the actual address of the command is /usr/local/mysql/bin/mysql, but a soft link is made to the file to the /usr/bin/ directory, so there is no need to use absolute The command can be called via the path.

Let’s make a file soft link ourselves to see if this is the case.

First create a file soft.sh

vim soft

#!/bin/bash
echo 'study softlink'

# chmod u+x soft

Then create a soft link to the file and see if it is possible to directly execute the command

# ln -s /root/soft  /usr/bin/
# soft
study softlink

Make a soft link to the directory

First of all, what we need to note is that when making a soft link, the target file or directory must not exist.

Below, we introduce a scenario, which is very common:

The website files are stored in the /www/wwwroot directory. As the system running time becomes worse and worse, the The directory is getting larger and larger. Since the directory is in the same partition as the root directory, the root directory is almost full. The other partition directory /data still has 99G of remaining space. At this time, if you don’t want to repartition, what should you do to liberate the partition where the root directory is located?

Below, we use directory soft links to solve this problem. First, create the wwwroot directory in the /data directory, then move all the files in the /www/wwwroot directory to /data/wwwfile, and then delete the /www/wwwroot directory. Finally, give the /data/wwwfile directory a soft link to /www/wwwroot

mkdir /data/wwwfile
mv /www/wwwroot/* /data/wwwfile
rm -rf /www/wwwroot
ln -s /data/wwwfile /www/wwwroot

Now, let’s test whether the problem is really solved. The root partition usage is currently 19%, while the /data partition usage is 9%.

/dev/vda1        40G  7.4G   33G  19% /
/dev/vdb1       500G   43G  458G   9% /data

We create a 2G large file in the /www/wwwroot directory, and then see which partition's capacity has increased. The answer we want is that the /data partition usage has become higher, while the root partition has not changed.

dd if=/dev/zero of=/www/wwwroot/bigfile bs=1G count=2
# 然后看看各分区使用容量的变化
/dev/vda1        40G  7.4G   33G  19% /
/dev/vdb1       500G   45G  456G   9% /data

As you can see, the available capacity of the root partition is still 33G, but the /data partition has changed from 458G to 456G, so we have solved the problem.

The above is the detailed content of Introduction to Linux file links and their applications. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn