Linux硬链接、软链接的介绍及两者的区别

一、硬链接

链接数就是名字的数量:

ln(link) 硬链接,多个相同的inode文件(同分区),多个硬链接文件的存在在硬盘上只占用一个文件的容量

创建的硬链接是平等的,inode相同,删除某一个互不影响,其他文件仍然可以使用

硬链接的本质:给一个文件创建多个名字

 

创建多个硬链接,观察链接数的变化(+1),链接数是几就是有几个文件名

同一分区中,同一个inode号必定是一个文件


原始的文件

[root@CentOS7 testdir]# ll -i man.txt
15 -rw-r--r--.  1 root root 15978 Aug  8  2008 man.txt

创建第一个硬链接

[root@CentOS7 testdir]# ln man.txt test/f11
[root@CentOS7 testdir]# ll -i man.txt  test/f11 
15 -rw-r--r--. 2 root root 15978 Aug  8  2008 man.txt
15 -rw-r--r--. 2 root root 15978 Aug  8  2008 test/f11

创建第二个硬链接

[root@CentOS7 testdir]# ln man.txt testdir/f22
[root@CentOS7 testdir]# ll -i man.txt testdir/f22 test/f11 
15 -rw-r--r--. 3 root root 15978 Aug  8  2008 man.txt
15 -rw-r--r--. 3 root root 15978 Aug  8  2008 testdir/f22
15 -rw-r--r--. 3 root root 15978 Aug  8  2008 test/f11

当对一个文件创建多个硬链接时,所有文件的inode相同,权限、大小、时间等属性相同。

[root@CentOS7 testdir]# echo "aaaaaaaaaaaaaaaaaaa" >man.txt
[root@CentOS7 testdir]# ll -i testdir/f22 test/f11 man.txt test/f
15 -rw-r--r--. 4 root root 8 Jul 29 09:49 man.txt
15 -rw-r--r--. 4 root root 8 Jul 29 09:49 testdir/f22
15 -rw-r--r--. 4 root root 8 Jul 29 09:49 test/f
15 -rw-r--r--. 4 root root 8 Jul 29 09:49 test/f11

当向一个文件写入数据,其他文件的属性内容等也会发生变化


硬链接不能跨分区,跨设备创建

[root@CentOS7 testdir]# ln man.txt /roo/a
ln: creating hard link `/roo/a' => `man.txt': No such file or directory


硬链接不能针对目录

[root@CentOS7 testdir]# ln Help/ H
ln: `Help/': hard link not allowed for directory


当删除原始文件后,链接文件仍然可以查看

[root@CentOS7 testdir]# ll -i man.txt man
131 -rw-r--r--. 6 root root 3256 Aug  1 16:54 man
131 -rw-r--r--. 6 root root 3256 Aug  1 16:54 man.txt
[root@CentOS7 testdir]# rm -f man.txt 
[root@CentOS7 testdir]# tail man
  -Z, --ditroff              use groff and force it to produce ditroff
 
  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version
    
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
 
Report bugs to cjwatson@debian.org.

二、软(soft)链接:

软链接相当于Windows的快捷方式

[root@CentOS7 testdir]# ln -s man.txt man
[root@CentOS7 testdir]# ls -li man.txt man
12 lrwxrwxrwx. 1 root root 7 Jul 29 10:04 man -> man.txt
15 -rw-r--r--. 4 root root 8 Jul 29 09:49 man.txt

创建软链接文件,链接文件会以绿色字体标识并指向原文件,通过观察可以发现两个文件的inode编号不同

 

对原始文件创建软链接文件

[root@CentOS7 testdir]# ln –s man.txt /roo/man.txt
[root@CentOS7 testdir]# ll /root/man.txt
lrwxrwxrwx. 1 root root  7 Aug  1 16:55  /root/man.txt -> man.txt
[root@CentOS7 testdir]# ln –s ../../testdir/man.txt /root/man1
[root@CentOS7 testdir]# ll /root/man1
lrwxrwxrwx. 1 root root  9 Aug  1 16:56 /root/man1 -> ../../testdir/man.txt
 
[root@CentOS7 testdir]# ln -s /testdir/ /root/test1
[root@CentOS7 testdir]# ll -d /testdir/ /root/test1
lrwxrwxrwx. 1 root root  9 Aug  1 17:02 /root/test1 -> /testdir/
drwxr-xr-x. 3 root root 34 Aug  1 16:53 /testdir/

通过上面的实例可以发现软链接可以针对目录,跨分区创建,并且创建的时候要注意路径的问题,如果路径错误,链接文件会保存显示的

软链接时需要注意绝对路径和相对路径,相对于软链接的路径而不是当前目录的路径(指向相对于当前工作目录或某目录的位置)


当删除原始文件后,创建的软链接文件将不能访问

[root@CentOS7 testdir]# ln -s /testdir/man /root/111111
[root@CentOS7 testdir]# ll -i /testdir/man /root/111111
105103873 lrwxrwxrwx. 1 root root   12 Aug  1 17:16 /root/111111 -> /testdir/man
      131 -rw-r--r--. 5 root root 3256 Aug  1 16:54 /testdir/man
[root@CentOS7 testdir]# rm -f /testdir/man 
[root@CentOS7 testdir]# ll /testdir/man /root/111111 
ls: cannot access /testdir/man: No such file or directory
lrwxrwxrwx. 1 root root 12 Aug  1 17:16 /root/111111 -> /testdir/man
[root@CentOS7 testdir]# cat /root/111111 
cat: /root/111111: No such file or directory

三、软硬链接的区别:

软链接和硬链接的区别主要在于:删除原始文件后,软链接将失效,但硬链接仍然可用;软链接适用于文件或目录,但硬链接只可用于文件,不能为目录建立硬链接;软链接与原始文件可以位于不同的文件系统中,但硬链接必须与原始文件在同一文件系统(如一个Linux分区)内。

 

 

原创文章,作者:cyh5217,如若转载,请注明出处:http://www.178linux.com/29045

(0)
cyh5217cyh5217
上一篇 2016-08-04
下一篇 2016-08-04

相关推荐

  • linux中硬链接与软链接的区别

    硬连接和软链接的区别: 1、          首先,两者的作用对象不同: 硬链接,只能应用于文件,而不能应用于目录,而且不能跨文件系统(即分区)。 软(符号)链接,可以应用于文件,而且可以应用于目录和可跨文件系统(分区)。 2、   &n…

    Linux干货 2016-10-20
  • proxy_pass转发路径

    一、引言 在nginx中配置proxy_pass时,proxy_pass后面的路径最后面加“/”和不加“/”会有所区别。加“/”时,nginx不会代理location部分,不加“/”时,nginx会同时代理location部分。下面通过实验来证明。 二、实验 实验环境简要说明:     node1为httpd服务器(1…

    Linux干货 2017-01-12
  • 命令read

    read命令是一个bash命令,它用于从键盘或标准输入中读取文本.我们可以使用read以交互的形式读取来自用户的输入.并且read还提供一种不需要按回车就能够输入参数的方法. read可以从标准输入中读取单独的一行,或者使用-u选项,从文件描述符FD中读取.并且这单独的行被分隔成多个域,第一个词被赋值给第一个变量,第一个赋值给第二个变量,以此类推,直到剩下的…

    Linux干货 2016-08-12
  • 浅谈Linux中的用户和组

    又到了周六,日常写博客的日子。 上周模模糊糊的写了第一篇博客,大概知道了怎么写,但是这周就比以往不同了,脑子中已经有了思路,那写起来就会更加详细易懂。 这周学了很多知识点,但是我想对Linux 用户和组进行详细的描述。 一、用户(Username/UID) 用户分为两种:管理员和普通用户     管理员root  UI…

    2017-07-22
  • keepalived+nginx 实现 sharepoint 负载均衡

    keepalived 实现虚拟路由
    nginx 做反向代理和负载均衡

    Linux干货 2017-12-20
  • 分布式系统介绍及MogileFS安装、基本配置

    分布式系统介绍及MogileFS安装、基本配置 分布式 MogileFS 前言: 什么是分布式? 分布式存在的意义? 分布式的难点及CAP、BASE、2PC、X/Open XA介绍 分布式存储和分布式文件系统: MogileFS实现原理: MogileFS编译安装和配置 总结 前言: 不知不觉中我们就进入大数据时代, 什么是大数据? 什么是分布式?…

    Linux干货 2016-05-04