2019独角兽企业重金招聘Python工程师标准>>>

在Linux系统中,内核为每一个新创建的文件分配一个Inode(索引结点),每个文件都有一个惟一的inode号。文件属性保存在索引结点里,在访问文件时,索引结点被复制到内存在,从而实现文件的快速访问。

硬链接说白了是一个指针,指向文件索引节点,系统并不为它重新分配inode。可以用:ln命令来建立硬链接。语法:

ln [options] existingfile newfile
ln [options] existingfile-list directory

用法:

第一种:为”existingfile”创建硬链接,文件名为”newfile”。

第二种:在”directory”目录中,为 ”existingfile-list”中包含的所有文件创建一个同名的硬链接。常用可选[options] –f 无论”newfile”存在与否,都创建链接。-n 如果”newfile”已存在,就不创建链接。

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

  • 文件有相同的 inode 及 data block;
  • 只能对已存在的文件进行创建;
  • 不能交叉文件系统进行硬链接的创建;
  • 不能对目录进行创建,只可对文件创建;
  • 删除一个硬链接文件并不影响其他有相同 inode 号的文件。

软链接与硬链接不同,若文件用户数据块中存放的内容是另一文件的路径名的指向,则该文件就是软连接。软链接就是一个普通文件,只是数据块内容有点特殊。软链接有着自己的 inode 号以及用户数据块(见 图 2.)。因此软链接的创建与使用没有类似硬链接的诸多限制:

  • 软链接有自己的文件属性及权限等;
  • 可对不存在的文件或目录创建软链接;
  • 软链接可交叉文件系统;
  • 软链接可对文件或目录创建;
  • 创建软链接时,链接计数 i_nlink 不会增加;
  • 删除软链接并不影响被指向的文件,但若被指向的原文件被删除,则相关软连接被称为死链接(即 dangling link,若被指向路径文件被重新创建,死链接可恢复为正常的软链接)。

(1)由于不同的分区会有相同的inode及硬连接的特殊性和文件系统的可卸载性,导致硬连接不容许跨文件系统(分区)!而 soft link 具有完整的 pathname,所以他可以跨越不同文件系统。

(2) 硬连接不会建产新的inode,硬连接不管有多少个,都指向的是同一个inode节点,只是新建一个hard link会把结点连接数增加,只要结点的连接数不是0,文件就一直存在,不管你删除 的是源文件还是连接的文件。只要有一个存在,文件就存在(其实也不分什么源文件连接文件的,因为他们指向都是同一个 inode节点)。当你修改源文件或者连接文件任何一个的时候,其他的文件都会做同步的修改 。

软链接不直接使用inode节点号作为文件指针,而是使用文件路径名作为指针。所以删除连接文件对源文件无影响,但是删除源文件,连接文件就会找不到要指向的文件。软链接有自己的inode,并在磁盘上有一小片空间存放路径名.

(3)软连接可以对一个不存在的文件名进行连接。

(4)软连接可以对目录进行连接。

ln -s source dist        # 建立软连接 
    ln source dist           # 建立硬连接

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

    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:

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.

转载于:https://my.oschina.net/u/347414/blog/268753

Hard link and soft link in Linux相关推荐

  1. Linux下的hard link和soft link

    Linux中包括两种链接:硬链接(hard link)和软链接(soft link),软链接又称为符号链接(symbolic link) 创建命令: ln -s softlink destfile/d ...

  2. linux hard link和soft link(硬链接和软链接)的简单介绍

    文件由何决定? 首先需要说明linux的文件的概念,我们如何分辨两个在linux里边的文件(以下简称文件)是不同的文件?由此就不得不说到一个东西,inode. 根据维基的定义: inode是指在许多& ...

  3. 通过inode理解hard link 和 soft link

    inode

  4. 软链接(Soft Link,符号链接)和硬链接(Hard Link)。

    目录 引言 创建软连接,硬链接 创建语句 二者区别 引言 链接是一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法.方便文件的共享使用,在Linux操作系统中引入了连接,链接被分为两种:软 ...

  5. Smart Link 与Monitor Link

    原理概述: 在以太网络中,为了提高网络的可靠性,通常采用双归属上行方式进行组网,即一台交换机同时连接两台上行交换机,但是在二层网络中可能会带来环路问题.为了解决环路问题,可以采用STP技术,但STP的 ...

  6. 华为数通笔记-Smart Link和Monitor Link

    Smart Link和Monitor Link简介 定义 Smart Link,又叫做备份链路.一个Smart Link由两个接口组成,其中一个接口作为另一个的备份.Smart Link常用于双上行组 ...

  7. Smart Link和Monitor Link

    Smart Link和Monitor Link Smart Link 简单的说就是实现链路负载,或者理解可将自定义的数据流走哪条链路. Monitor Link英文的解释就是他的意思,用来做上下端口联 ...

  8. Smart Link与Monitor Link的配置

    一.原理简述 在以太网的网络中,为了提高网络的可靠性,一般采用双归属上行方式进行组网,就是指交换机同时连接着两台上行交换机,可是存在一些问题,例如二层网络中可能存在环路问题.其解决方法是采用STP技术 ...

  9. 交换机-Smart Link AND Monitor Link的配置

            在网络实际运用中,为了对网络的可靠性实现提高,通常采用双归属上行方式进行组网--一台交换机同时连接两台上行交换机.这里可能会造成环路问题,为了解决环路问题,学了前面的知识,我们知道可以 ...

最新文章

  1. Windows 8 / 8.1 禁用驱动签名最详细图文教程
  2. 应用程序进程(三):创建消息循环
  3. HttpRequestException encountered解决方法
  4. linux c/c++
  5. OpenID 和 OAuth 的区别及第三方登录的安全隐患分析
  6. paip.提升效率---提升绑定层次--form绑定取代field绑定
  7. Linux之系统操作命令
  8. 什么是spooling技术 他有哪几部分组成_气调保鲜冷库有哪几部分组成?
  9. 《Android Studio开发实战 从零基础到App上线(第2版)》常见问题解答
  10. Halcon PDF文档(extension_package_programmers_manual)学习总结
  11. 给图片添加水印效果图的函数(可以在图片上添加自己的版权和LOGO图片的水印) 【转载】...
  12. MFC的多国语言界面的实现
  13. 二叉排序树(概念,查找,插入,删除)
  14. Mac 屏幕共享和远程控制
  15. 腾讯测试岗位的面试经历
  16. UML需求分析步骤实例解析
  17. Github上8个很棒的Vue项目
  18. 第11章 角色页的删除、批量删除
  19. Nature:13个维度,手把手教“研究er”如何做学术报告
  20. windows系统卸载VMware Workstation 并删除注册表残留信息

热门文章

  1. https请求报Connection reset问题
  2. 如何选择自己心仪的U盘
  3. 原力计划S5上榜博主名单公布(第四期已更新)
  4. 《喜羊羊与灰太狼》中羊村的团队建设
  5. 商标复审成功率是多少?
  6. parent.layer.open打开的页面向上个页面传值
  7. 编写一个制造各种车辆的程序。包含三个类,具体要求如下: (1)基类Vehicle,包含轮子数和汽车自身重量两个属性,一个两参数的构造方法,一个显示汽车信息的方法; (2)小轿车类Car,增加载客数属性
  8. QQ空间自动点赞脚本
  9. 只需8步,轻松构建用户画像标签体系
  10. 基于忆阻器的神经网络应用研究!道翰天琼认知智能机器人平台API接口大脑为您揭秘。