Linux2.6软链接、硬链接
软连接文件
[root@chy002 ~]# ls -l /bin lrwxrwxrwx. 1 root root 7 10月 17 06:15 /bin -> usr/bin [root@chy002 ~]# /bin/ls anaconda-ks.cfg [root@chy002 ~]# /usr/bin/ls anaconda-ks.cfg
软连接命令
ln -s 源文件 软链接文件
[root@chy002 ~]# ls -lsa /etc/yum.log #文件 0 lrwxrwxrwx 1 root root 12 10月 26 05:34 /etc/yum.log -> /tmp/yum.log [root@chy002 ~]# ls -lsa /etc/chy #目录 0 lrwxrwxrwx 1 root root 8 10月 26 05:35 /etc/chy -> /tmp/chy #做软连接要用绝对路径
软链接是建立一个独立的文件,作用是当读取这个链接文件的时候,会把读取的行为转发到该文件所link的文件上。可以链接目录文件,可以跨分区。源文件删除后,链接文件失效。
软连接应用实例,当某分区被某服务的日志文件快写满的时候,可以把该日志文件拷贝到空闲磁盘,并删除该文件,在该分区创建一个同名的软连接文件。
[root@chyuanliu-01 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 861M 17G 5% / devtmpfs 489M 0 489M 0% /dev tmpfs 494M 0 494M 0% /dev/shm tmpfs 494M 6.7M 487M 2% /run tmpfs 494M 0 494M 0% /sys/fs/cgroup /dev/sda1 197M 75M 123M 38% /boot [root@chyuanliu-01 ~]# nginx /boot/nginx.log^C [root@chyuanliu-01 ~]# /boot/nginx.log mv -> /var/log/nginx.log^C [root@chyuanliu-01 ~]# rm /boot/nginx.log ^C [root@chyuanliu-01 ~]# ln -s /var/log/nginx.log /boot/nginx.log^C
再来看一下,软连接文件、源文件及Inode的关系。读取过程大概如下:由链接文件的Inode读取到链接文件的内容仅有文件名,根据文件名链接到正确的目录去取得目标的inode,最终才能读取到正确的数据。
[root@chy002 tmp]# ls -ls passwd.txt 4 -rw-r--r-- 1 root root 965 10月 26 05:51 passwd.txt [root@chy002 tmp]# ln -s /tmp/passwd.txt /tmp/chy chy/ chy2/ chy2.txt [root@chy002 tmp]# ln -s /tmp/passwd.txt /tmp/chy/p1.txt [root@chy002 tmp]# ls -ls /tmp/chy/p1.txt #链接文件inode为1 0 lrwxrwxrwx 1 root root 15 10月 26 05:52 /tmp/chy/p1.txt -> /tmp/passwd.txt [root@chy002 tmp]# ls -ls passwd.txt #源文件inode为1 4 -rw-r--r-- 1 root root 965 10月 26 05:51 passwd.txt [root@chy002 tmp]# echo "0"> /tmp/chy/p1.txt #修改链接文件,源文件内容也变化 [root@chy002 tmp]# cat passwd.txt 0 [root@chy002 tmp]# rm passwd.txt #删除源文件,链接文件无效 rm:是否删除普通文件 "passwd.txt"?y [root@chy002 tmp]# cat chy/p1.txt cat: chy/p1.txt: 没有那个文件或目录
硬链接文件
硬链接直接使用了和源文件相同的Inode,直接链接到文件放置的块区域。进行硬链接的时候实际上该文件内容没有任何变化,只是指定了相同的inode。不消耗inode不消耗block数量。两个相互为硬链接。删除一个另一个还可以用。
限制:不能跨文件系统,不同文件系统有不同的Inode table;不能链接目录。
[root@chy002 tmp]# ln 123.txt 321.txt [root@chy002 tmp]# ls -ls 123.txt 321.txt 4 -rw-r--r-- 2 root root 965 10月 26 06:06 123.txt 4 -rw-r--r-- 2 root root 965 10月 26 06:06 321.txt [root@chy002 tmp]# ls -lsh 123.txt 321.txt 4.0K -rw-r--r-- 2 root root 965 10月 26 06:06 123.txt 4.0K -rw-r--r-- 2 root root 965 10月 26 06:06 321.txt
浙公网安备 33010602011771号