ln


命令说明

链接文件(将某个文件链接到一个文件上)

语法:

ln [-sf] 源文件目标文件

ln [-sf] 源文件....  目标目录(将一个或多个文件链接到一个目录上)

参数:

-s :如果不加任何参数就是进行连接,那就是hard link,至于-s就是symblioc link

-f :如果目标文件存在时,就主动将目标文件直接删除后再创建

-d :允许系统管理者硬链接自己的目录

-i :在删除与目标文件同文件名的文件时先进行询问

-n :在进行软链接时,将目标文件视为一般的文件

-v :在链接之前显示其文件名

-b :将在链接时会被覆盖或删除的文件进行备份


·hard link(硬链接或实际链接)

每个文件都会占用一个inode,文件内容由inode的记录来指向

想要读取该文件,必须经过记录的文件名来指向到正确的inode号码才能读取

hard link只是在某个目录下新建一条文件名连接到某inode号码的关联记录而已

如果将任何一个文件名删除,其实inodeblock都还存在的

硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

hard link的限制:

1.不能跨文件系统

2.不能链接到目录

·symbolic link (符号连接,也即快捷方式)

 symblolic link就是在创建一个独立的文件,而这个文件会让数据的读取指向他连接的那个文件的文件名

源文件被删除后,symbolic link的文件会打不开,实际上是找不到源文件的文件名,

symbolic link所创建的文件为一个独立的新文件,所以会占用掉inodeblock

命令实践:


[root@yubing ~]# ln /etc/crontab crontab1     创建hard link

You have mail in /var/spool/mail/root

[root@yubing ~]# ll -i /etc/crontab crontab1     会发现硬链接与链接文件基本相同

487860 -rw-r--r-- 2 root root 255 Jan  6  2007 crontab1

487860 -rw-r--r-- 2 root root 255 Jan  6  2007 /etc/crontab

[root@yubing ~]# ln -s /etc/crontab crontab2    -加上-s参数,创建symbolic link

[root@yubing ~]# ll -i /etc/crontab crontab2

780299 lrwxrwxrwx 1 root root  12 Apr  8 04:06 crontab2 -> /etc/crontab

487860 -rw-r--r-- 2 root root 255 Jan  6  2007 /etc/crontab   只是一个快捷方式

[root@yubing ~]# touch yubing.txt  

[root@yubing ~]# ll

total 76

-rw------- 1 root root   887 Apr  7 01:47 anaconda-ks.cfg

-rw-r--r-- 2 root root   255 Jan  6  2007 crontab1

lrwxrwxrwx 1 root root    12 Apr  8 04:06 crontab2 -> /etc/crontab

-rw-r--r-- 1 root root 23947 Apr  7 01:47 install.log

-rw-r--r-- 1 root root  3619 Apr  7 01:46 install.log.syslog

drwxr-xr-x 7 root root  4096 Oct 28  2011 oldboy

-rw-r--r-- 1 root root   425 Apr 14 14:13 oldboy.tar.gz

-rw-r--r-- 1 root root     0 Apr 17 00:50 yubing.txt

[root@yubing ~]# ln yubing.txt link1    

[root@yubing ~]# ln -s yubing.txt link2   创建硬链接和软连接都链接到yubing,txt

[root@yubing ~]# ll

total 84

-rw------- 1 root root   887 Apr  7 01:47 anaconda-ks.cfg

-rw-r--r-- 2 root root   255 Jan  6  2007 crontab1

lrwxrwxrwx 1 root root    12 Apr  8 04:06 crontab2 -> /etc/crontab

-rw-r--r-- 1 root root 23947 Apr  7 01:47 install.log

-rw-r--r-- 1 root root  3619 Apr  7 01:46 install.log.syslog

-rw-r--r-- 2 root root     0 Apr 17 00:50 link1

lrwxrwxrwx 1 root root    10 Apr 17 00:50 link2 -> yubing.txt

drwxr-xr-x 7 root root  4096 Oct 28  2011 oldboy

-rw-r--r-- 1 root root   425 Apr 14 14:13 oldboy.tar.gz

-rw-r--r-- 2 root root     0 Apr 17 00:50 yubing.txt

[root@yubing ~]# rm yubing.txt

rm: remove regular empty file `yubing.txt'? y

[root@yubing ~]# ll    删除yubing.txt后,hard link还存在,软链接却提示链接的文件

total 80                                            不存在了

-rw------- 1 root root   887 Apr  7 01:47 anaconda-ks.cfg

-rw-r--r-- 2 root root   255 Jan  6  2007 crontab1

lrwxrwxrwx 1 root root    12 Apr  8 04:06 crontab2 -> /etc/crontab

-rw-r--r-- 1 root root 23947 Apr  7 01:47 install.log

-rw-r--r-- 1 root root  3619 Apr  7 01:46 install.log.syslog

-rw-r--r-- 1 root root     0 Apr 17 00:50 link1

lrwxrwxrwx 1 root root    10 Apr 17 00:50 link2 -> yubing.txt  会闪烁报错

drwxr-xr-x 7 root root  4096 Oct 28  2011 oldboy

-rw-r--r-- 1 root root   425 Apr 14 14:13 oldboy.tar.gz

[root@yubing ~]#