软链接
软链接又称之为符号连接(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