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

链接是一种在共享文件和访问它的用户的若干目录项之间建立联系的方法


说明: 

    ln命令可以创建硬链接或软链接文件


语法:

 ln [OPTION]... [-T] TARGET LINK_NAME

 ln [OPTION]... TARGET              

 ln [OPTION]... TARGET... DIRECTORY    

 ln [OPTION]... -t DIRECTORY TARGET...  

 

选项;

-i:删除文件前进程确认

-s:创建符号链接文件而不是硬链接文件

-d:允许超级用户创建指向目录的硬链接

-f:强行删除任何已存在的目标文件

-n:如果目的地是一个链接至某目标的符号链接,会将该符号链接当作普通文件处理,先将该已存在的链接备份或删除

-L:将硬链接创建符号链接引用

-t<目录>:在指定目录中创建普通文件

-T:将链接名称当作普通文件

-v:链接前先列出每个文件的名称

 

硬链接

链接数就是名字的数量:

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.

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

 

二、软链接

软链接又称为符号链接,软连接相当于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分区)内。