2.21 软链接文件

软链接介绍

  • 链接文件分为硬链接文件(hard link)和软链接文件(symbolic link),两种链接的本质区别在于inode。
    • 软链接文件:是建立一个独立的文件,当读取这个链接文件时,它会把读取的行为转发到该文件所链接的文件上。做软链接尽量使用绝对路径。

例子

```
[root@hf-01 ~]# ls -l /bin     这个/bin,就是一个软链接文件,真正是在/usr/bin/下
lrwxrwxrwx. 1 root root 7 8月  21 05:03 /bin -> usr/bin
[root@hf-01 ~]# /bin/ls
11.txt	123  234  2.txt  33.txt  anaconda-ks.cfg.1  ha.txt  hf
[root@hf-01 ~]# /usr/bin/ls     会发现和/bin/ls是一个软链接文件
11.txt	123  234  2.txt  33.txt  anaconda-ks.cfg.1  ha.txt  hf
```
  • 软链接其实就是在一个文件本身存了另一个文件目录的路径。链接文件的大小适合路径是有关系的,路径越长,那这个软链接文件越大
    在/lib64/目录中软链接文件最多
    [root@hf-01 ~]# ls -l /lib64/
    
    

软链接优势

软链接就像是windows中的快捷方式,可以很大程度的节省磁盘空间

案例分析
比如要找一个文件,必须要在/tmp/123/12/1.txt,而本身这个1.txt文件放到了/root/123/这个目录下
而这个程序必须要在/tmp/12/目录下找到1.txt文件

方法一:可以拷贝一份到该目录下,但若是文件更改后,就会继续要拷贝,所以很麻烦。

方法二:给/root/123/1.txt做一个软链接
ln -s创建软链接
  • ln -s 源文件 软链接文件
[root@hf-01 ~]# ls
11.txt  123  234  2.txt  33.txt  anaconda-ks.cfg.1  ha.txt  hf
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# ls
aminglinux  amning  mysql.sock  user1  yum.log  yunwei
[root@hf-01 tmp]# ln -s /tmp/yum.log /root/123/yum.log
[root@hf-01 tmp]# ls -l /root/123/      这样就形成了一个软链接文件
总用量 0
lrwxrwxrwx. 1 root root 12 10月 30 05:44 yum.log -> /tmp/yum.log
ln -s 链接目录
  • 不仅可以链接文件,还可以链接目录
[root@hf-01 tmp]# ln -s /tmp/amning/ /root/amning111
[root@hf-01 tmp]# ls -l !$
ls -l /root/amning111
lrwxrwxrwx. 1 root root 12 10月 30 05:51 /root/amning111 -> /tmp/amning/

同一目录,创建软链接
  • 在/tmp/目录下,源文件aa.log链接——》目标文件ss.log
[root@hf-01 tmp]# ls -l
总用量 4
-rw-r--r--. 1 root    root     0 10月 30 06:09 aa.log
srwxrwxrwx. 1 mysql   mysql    0 10月 30 04:49 mysql.sock
drwxrwxrwx. 2 user1   user1    6 10月 27 09:07 user1
[root@hf-01 tmp]# ln -s aa.log ss.log
[root@hf-01 tmp]# ls -l
总用量 4
-rw-r--r--. 1 root    root     0 10月 30 06:09 aa.log
srwxrwxrwx. 1 mysql   mysql    0 10月 30 04:49 mysql.sock
lrwxrwxrwx. 1 root    root     6 10月 30 06:13 ss.log -> aa.log
弊端——在同一目录下
  • 弊端——假设做了一个相对路径的软链接,再把所有的文件拷贝到另一台机器上的时候(软链接本身也是一个文件),结果会发现这个文件不存在,(也就是源不存在),所以尽量使用绝对路径
弊端解决方法
  • 尽量使用绝对路径,这样就会避免文件的不存在
[root@hf-01 ~]# cd /tmp/
[root@hf-01 tmp]# ls -l
总用量 4
drwxr-xr-x. 4 user1   hanfeng 53 10月 30 06:01 aminglinux
lrwxrwxrwx. 1 root    root     6 10月 30 06:13 ss.log -> aa.log
[root@hf-01 tmp]# mv ss.log aminglinux/
[root@hf-01 tmp]# cd aminglinux
[root@hf-01 aminglinux]# ls -l
总用量 0
drwxr-xr-x. 4 user1 hanfeng 31 10月 25 06:55 aming2
lrwxrwxrwx. 1 root  root     6 10月 30 06:13 ss.log -> aa.log

这里会发现aa.log在飘红闪烁,这是因为这个文件不存在

阿铭工作中例子

[root@hf-01 ~]# df -h    查看磁盘分区情况
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        18G  2.4G   16G   14% /
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
  • 假如在/boot下,一个hanfeng.log日志文件占用很大空间,但是这个yin.log日志文件必须在这个/boot目录下,不能更改文件路径,那么就可以使用软链接来完美解决这个问题。可以将hanfeng.log 复制到/根目录下(大磁盘空间下)cp /boot/hanfeng.log /hanfeng.log 然后删除/boot/hanfeng.log文件,尽快的将复制的那个日志做个软链接链接到/boot,ln -s /hanfeng.log /boot/hanfeng.log就可以解决文件过大但是不能改路径的问题。

总结

  • 命令ln(link)
  • 格式:ln【-s】 【来源文件】【目的文件】
  • -s 建立软链接,不加-s建立硬链接。

转载于:https://my.oschina.net/u/3707314/blog/1556562

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值