软链接作用及使用实战

软链接

软链接又称之为符号连接(Symbolic Link)。软链接文件类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。

ln -s 源文件名 链接名

实战

eg:/home/test/hello文件打印hello,world,想在任意文件夹下输入简单字符即可执行(如下例hello2),不想每次都输入/home/test/hello这样的长串,应该怎么做?

#!/usr/bin/python
# encoding=utf8
import os

def soft_link():
    rc = 1
    if not(os.path.exists('/usr/local/bin/hello2')):
        rc = os.system("ln -s /home/test/hello /usr/local/bin/hello2")
    else:
        #hello2已存在,要重新更新
        os.system("rm -rf /usr/local/bin/hello2")
        rc = os.system("ln -s /home/test/hello usr/local/bin/hello2")
    if rc!=0:
        print"error!"
        return rc
    else:
        return rc

def main():
    soft_link()

main()

此时,/usr/local/bin 下生成了hello2软连接,在任意文件夹执行hello2,发现还是有问题Permission denied。
这是因为源文件hello还不能./hello执行

chmod +x hello

hello可以./hello执行,此时软连接hello2->/home/test/hello变绿
实战到此就完成了,在任意文件下,输入hello2,即可打印hello,world

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十年23

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值