软连接和硬连接区别

本文详细解释了Unix系统中硬链接与软链接的工作原理及使用场景。通过具体实例展示了两种链接的区别,包括文件修改、删除操作的影响以及跨文件系统的链接创建等。

http://www.ugrad.cs.ubc.ca/~cs219/CourseNotes/Unix/commands-links.html 

Hard links and Soft links

Links

As was mentioned in the section on file system structure, every file has a data structure (record) known as an i-node that stores information about the file, and the filename is simply used as a reference to that data structure. A link is simply a way to refer to the contents of a file. There are two types of links:

  • Hard links : a hard link is a pointer to the file's i-node. For example, suppose that we have a file a-file.txt that contains the string "The file a-file.txt":
    % cat a-file.txt
    The file a-file.txt
    %
    

    Now we use the ln command to create a link to a-file.txt called b-file.txt :

    % ls
    ./  ../  a-file.txt
    % ln a-file.txt b-file.txt
    % ls
    ./  ../  a-file.txt  b-file.txt
    

    Hard Links

    The two names a-file.txt and b-file.txt now refer to the same data:

    % cat b-file.txt
    The file a-file.txt
    %
    

    If we modify the contents of file b-file.txt , then we also modify the contents of file a-file.txt :

    % vi b-file.txt
    ...
    % cat b-file.txt
    The file a-file.txt has been modified.
    % cat a-file.txt
    The file a-file.txt has been modified.
    %
    

    and vice versa:

    % vi a-file.txt
    ...
    % cat a-file.txt
    The file a-file.txt has been modified again!
    % cat b-file.txt
    The file a-file.txt has been modified again!
    %
    

  • Soft links (symbolic links) : a soft link, also called symbolic link, is a file that contains the name of another file. We can then access the contents of the other file through that name. That is, a symbolic link is like a pointer to the pointer to the file's contents. For instance, supposed that in the previous example, we had used the -s option of the ln to create a soft link:
    % ln -s a-file.txt b-file.txt
    
    On disk, the file system would look like the following picture:

    Soft Links

But what are the differences between the two types of links, in practice? Let us look at an example that highlights these differences. The directory currently looks like this (let us assume that a-file.txt b-file.txt are both hard links to the same file):

% ls
./  ../  a-file.txt  b-file.txt

Let us first add another symbolic link using the -s option:

% ln -s a-file.txt Symbolicb-file.txt
% ls -F
./  ../  a-file.txt  b-file.txt  Symbolicb-file.txt@

A symbolic link, that ls -F displays with a @ symbol, has been added to the directory. Let us examine the contents of the file:

% cat Symbolicb-file.txt 
The file a-file.txt has been modified again!

If we change the file Symbolicb-file.txt , then the file a-file.txt is also modified.

% vi Symbolicb-file.txt
...
% cat Symbolicb-file.txt
The file a-file.txt has been modified a third time!
% cat a-file.txt
The file a-file.txt has been modified a third time!
% cat b-file.txt
The file a-file.txt has been modified a third time!
%

If we remove the file a-file.txt , we can no longer access the data through the symbolic link Symbolicb-file.txt :

% ls -F
./  ../  a-file.txt  b-file.txt  Symbolicb-file.txt@
% rm a-file.txt
rm: remove `a-file.txt'? y
% ls -F
./  ../  b-file.txt  Symbolicb-file.txt@
% cat Symbolicb-file.txt
cat: Symbolicb-file.txt: No such file or directory

The link Symbolicb-file.txt contains the name a-file.txt , and there no longer is a file with that name. On the other hand, b-file.txt has its own pointer to the contents of the file we called a-file.txt , and hence we can still use it to access the data.

% cat b-file.txt
The file a-file.txt has been modified a third time!

Although it may seem like symbolic links are not particularly useful, hard links have their drawbacks. The most significant drawback is that hard links cannot be created to link a file from one file system to another file on another file system. A Unix file structure hierarchy can consist of several different file systems (possibly on several physical disks). Each file system maintains its own information regarding the internal structure of the system and the individual files on the system. Hard links only know this system-specific information, which make hard links unable to span file systems. Soft links, on the other hand, know the name of the file, which is more general, and are able to span file systems.

For a concrete analogy, suppose that our friend Joel User is a student at both UBC and SFU. Both universities assign him a student number. If he tries to use his UBC student number at SFU, he will not meet with any success. He will also fail if he tries to use his SFU student number at UBC. But if he uses his legal name, Joel User, he will probably be successful. The student numbers are system-specific (like hard links), while his legal name spans both of the systems (like soft links).

Here is an example that demonstrates a situation where a hard link cannot be used and a symbolic link is needed. Suppose that we try to create a hard link from the current working directory to the C header stdio.h .

% ln /usr/include/stdio.h stdio.h
ln: creating hard link `stdio.h' to `/usr/include/stdio.h': Invalid cross-device link
% 

The ln command fails because stdio.h is stored on a different file system. If we want to create a link to it, we will have to use a symbolic link:

% ln -s /usr/include/stdio.h stdio.h
% ls -l
lrwxrwxrwx    1 a1a1 guest          20 Apr 20 11:58 stdio.h -> /usr/include/stdio.h
% ls
./  ../  stdio.h@
% 

Now we can view the file stdio.h just as if it was located in the working directory. For example:

% cat stdio.h 
/* Copyright (c) 1988 AT&T */ 
/* All Rights Reserved */ 

/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */ 
/* The copyright notice above does not evidence any */ 
/* actual or intended publication of such source code. */ 

/* 
* User-visible pieces of the ANSI C standard I/O package. 
*/ 

#ifndef _STDIO_H 
#define _STDIO_H 
... 
% 

The entire output of the cat command was not included to save space.

Note that the long listing ( ls -l ) of a soft link does not accurately reflect its associated permissions. To view the permissions of the file or directory that the symbolic link references, the -L options of the ls command can be used. For example:

% ln -s /usr/include/stdio.h stdio.h

% ls -l stdio.h
lrwxrwxrwx   1 a1a1     undergrad     20 May 10 15:13 stdio.h -> /usr/include/stdio.h

% ls -l /usr/include/stdio.h
-rw-r--r--   1 root     bin        11066 Jan  5  2000 /usr/include/stdio.h

% ls -lL stdio.h
-rw-r--r--   1 root     bin        11066 Jan  5  2000 stdio.h

Knowledge of links becomes doubly important when working as part of a software development team, and even more so when using a source code management tool such as RCS. The man page for the ln command contains more information on Unix links and the ln .

### 连接硬连接区别 在 Linux 文件系统中,连接(symbolic link)硬连接(hard link)都是用于创建文件的额外入口的方式,但它们的工作机制特性存在显著差异。 #### 硬连接 (Hard Link) 硬连接是指向同一 inode 的多个文件名。这意味着当创建一个硬连接时,实际上是为现有文件增加了一个新的目录条目,这些条目共享相同的 inode 数据块[^1]。以下是硬连接的主要特点: - **inode 共享**:硬连接与其源文件共享同一个 inode 号,因此任何通过其中一个名称对文件的操作都会反映到其他名称上。 - **不可跨文件系统**:硬连接无法跨越不同的文件系统创建,因为不同文件系统有不同的 inode 编号空间[^4]。 - **不影响原始文件**:即使删除了原始文件,只要还有至少一个硬连接存在,文件的数据仍然保留并可通过剩余的硬连接访问。 示例命令如下: ```bash ln source_file hard_link_name ``` #### 连接 (Symbolic Link) 连接是一种特殊类型的文件,其中存储的是目标文件或目录的路径信息。它是独立存在的文件实体,具有自己的 inode 权限设置[^2]。以下是连接的关键特征: - **路径指向**:连接内部保存的是目标文件或目录的位置路径,而不是直接关联到其 inode。 - **可跨文件系统**:由于只是记录路径字符串,所以它可以轻松地横跨不同的文件系统工作。 - **依赖于目标文件的存在**:一旦目标文件被移除或者重命名,则此连接会成为悬挂链接(dangling link),即无效状态[^3]。 创建连接的例子: ```bash ln -s source_file symbolic_link_name ``` 综上所述,虽然两者都能提供到达某个特定资源的新途径,但在实际应用中有各自适用场景以及局限性需要注意。 ### 总结对比表 | 特性 | 硬连接 | 连接 | |---------------------|----------------------------|------------------------------| | 是否共享 inode | 是 | 否 | | 删除原文件的影响 | 数据仍存留 | 链接失效 | | 支持跨文件系统操作 | 不支持 | 支持 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值