zoukankan      html  css  js  c++  java
  • linux 软/硬链接详解

    SYNOPSIS

           ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)
           ln [OPTION]... TARGET                  (2nd form)
           ln [OPTION]... TARGET... DIRECTORY     (3rd form)
           ln [OPTION]... -t DIRECTORY TARGET...  (4th form)
    
    • form1:创建一个名为LINK_NAME 的连接文件指向原文件TARGET
    • form2:在当前目录中创建一个与TARGET同名的链接文件(TARGET不能在当前目录,目标可以是绝对路径,也可以是相对路径)
    • form3 | form4:在指定的目录DIRECTORY中,为每一个原文件TARGET创建一个链接文件。

    硬链接与软链接(-s)的联系与区别(默认是建立硬链接)

    我们知道文件都有文件名与数据,这在 Linux 上被分成两个部分:用户数据 (user data) 与元数据 (metadata)。用户数据,即文件数据块 (data block),数据块是记录文件真实内容的地方;而元数据则是文件的附加属性,如文件大小、创建时间、所有者等信息。在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名。文件名仅是为了方便人们的记忆和使用,系统或程序通过 inode 号寻找正确的文件数据块。图 1.展示了程序通过文件名获取文件内容的过程。
    图 1. 通过文件名打开文件

    由于硬链接是有着相同 inode 号仅文件名不同的文件,因此硬链接存在以下几点特性:

    • 文件有相同的 inode 及 data block;
    • 只能对已存在的文件进行创建;
    • 不能交叉文件系统进行硬链接的创建;
    • 不能对目录进行创建,只可对文件创建;
    • 删除一个硬链接文件并不影响其他有相同 inode 号的文件。
    软链接与硬链接不同,若文件用户数据块中存放的内容是另一文件的路径名的指向,则该文件就是软连接。软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的 inode 号以及用户数据块(见 图 2.)。因此软链接的创建与使用没有类似硬链接的诸多限制,软链接有以下属性: * 软链接有自己的文件属性及权限等; * 可对不存在的文件或目录创建软链接;(目标文件不存在则是无效链接) * 目标文件是相对路径的时候,移动软链接会失效,若是绝对路径则无影响,所以尽量使用绝对路径。 * 软链接可交叉文件系统; * 软链接可对文件或目录创建; * 创建软链接时,硬链接计数 i_nlink 不会增加; * 删除软链接并不影响被指向的文件,但若被指向的原文件被删除,则相关软连接被称为死链接(即 dangling link,若被指向路径文件被重新创建,死链接可恢复为正常的软链接)。 图 2. 软链接的访问 ![](https://img2018.cnblogs.com/blog/1494427/201809/1494427-20180930004153990-2110567826.png) 注意:使用相对路径建立软链接时候,原文件相对的不是执行命令时的路径而是相对于软链接所在目录的路径,而软链接的位置才是相对于当前命令的相对路径。 例如:Desktop目录下面有A,B 两个目录,如果要把A 目录中文件tem的软链接建立到B 目录中,在Desktop上层目录执行命令如下 `ln -s ../A/tem B` ![](https://img2018.cnblogs.com/blog/1494427/201810/1494427-20181001125539450-40450612.png)

    可选参数如下

    --backup[=CONTROL]
    make a backup of each existing destination file

       -b     like --backup but does not accept an argument
    
       -d, -F, --directory
              allow the superuser to attempt to hard link directories (note: will  probably  fail
              due to system restrictions, even for the superuser)
    
       -f, --force
              remove existing destination files
    
       -i, --interactive
              prompt whether to remove destinations
    
       -L, --logical
              dereference TARGETs that are symbolic links
    
       -n, --no-dereference
              treat LINK_NAME as a normal file if it is a symbolic link to a directory
    
       -P, --physical
              make hard links directly to symbolic links
    
       -r, --relative
              create symbolic links relative to link location
    
       -s, --symbolic
              make symbolic links instead of hard links
    
       -S, --suffix=SUFFIX
              override the usual backup suffix
    
       -t, --target-directory=DIRECTORY
              specify the DIRECTORY in which to create the links
    
       -T, --no-target-directory
              treat LINK_NAME as a normal file always
    
       -v, --verbose
              print name of each linked file
    
       --help display this help and exit
    
       --version
              output version information and exit
    

        每个文件存在两个计数器:i_count 与 i_nlink,即引用计数与硬链接计数。结构体 inode 中的 i_count 用于跟踪文件被访问的数量,而 i_nlink 则是上述使用 ls -l 等命令查看到的文件硬链接数。或者说 i_count 跟踪文件在内存中的情况,而 i_nlink 则是磁盘计数器。当文件被删除时,则 i_nlink 先被设置成 0。文件的这两个计数器使得 Linux 系统升级或程序更新变的容易。系统或程序可在不关闭的情况下(即文件 i_count 不为 0),将新文件以同样的文件名进行替换,新文件有自己的 inode 及 data block,旧文件会在相关进程关闭后被完整的删除。
    

    查看文件是否是硬链接

    $ touch file1       # 创建新文件 file1
    $ touch file2       # 创建新文件 file2
    $ ln file1 file3    # 为 file1 创建硬链接 file3
    $ ls -l
    total 0
    -rw-r--r-- 2 root root 0 Aug 12 16:59 file1
    -rw-r--r-- 1 root root 0 Aug 12 17:00 file2
    -rw-r--r-- 2 root root 0 Aug 12 16:59 file3
    

    结果的第二列数字就是指向该文件的硬链接数. 注意, 硬链接和原文件是无法区分的. 所以 file3 是 file1 的硬链接也可以看作 file1 是 file3 的硬链接. 所以该数字大于 2 即说明该文件是硬链接.

    查看文件的 inode number

    ls -i    # 可以与 ls -l 一起使用, 即 ls -il
    
    $ ls -il
    total 0
    267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file1
    267106 -rw-r--r-- 1 root root 0 Aug 12 17:00 file2
    267105 -rw-r--r-- 2 root root 0 Aug 12 16:59 file3
    

    这时结果的第一列就是文件的 inode number, 可以看出由于 file1 和 file3 互为硬链接, 所以他们的 inode number 相同.

    如何找出所有硬链接到某个文件的文件?

    首先使用

    1
    ls -i
    查看文件的 inode number

    然后使用
    find -inum
    查找所有指向该 inode 的文件

    例子:

    $ find . -inum 267105
    ./file3
    ./file1
    

    ref :https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/

  • 相关阅读:
    [LeetCode]2. Add Two Numbers链表相加
    Integration between Dynamics 365 and Dynamics 365 Finance and Operation
    向视图列添加自定义图标和提示信息 -- PowerApps / Dynamics365
    Update the Power Apps portals solution
    Migrate portal configuration
    Use variable to setup related components visible
    Loyalty management on Retail of Dynamic 365
    Modern Fluent UI controls in Power Apps
    Change screen size and orientation of a canvas app in Power App
    Communication Plan for Power Platform
  • 原文地址:https://www.cnblogs.com/ims-/p/9722540.html
Copyright © 2011-2022 走看看