代码之家  ›  专栏  ›  技术社区  ›  qht

Linux符号链接如何使当前目录中的目标文件成为自动绝对路径?

  •  0
  • qht  · 技术社区  · 6 年前

    cd ~
    mkdir bin
    export PATH=$PATH:bin
    mkdir -p projects
    cd projects
    echo 'hello world' > hello.sh
    chmod +x hello.sh
    ln -s hello.sh ~/bin/hello
    hello
    

    输出:

    -bash: hello: command not found
    

    我是如何改变的:

    ln -s hello.sh ~/bin
    hello.sh
    

    -bash: /home/qht/bin/hello.sh: Too many levels of symbolic links 
    

    我只是想看看发生了什么:

    ls -l ~/bin/hello.sh
    /home/qht/bin/hello.sh -> hello.sh 
    

    我的解决方法是:

    ln -sf $PWD/hello.sh ~/bin/hello
    ls ~/bin/hello
    /home/qht/bin/hello -> /home/qht/projects/hello.sh
    

    而且很有效,我也 为了看看是否有一个方便的选择,我发现:

    ln -sfr hello.sh ~/bin/hello
    ls -l ~/bin/hello
    /home/qht/bin/hello -> ../projects/hello.sh
    

    它起作用了,-r选项起作用了。

    如果ln-r可以自动将相对路径数据写入符号链接,为什么没有一个选项maybe-a来执行绝对路径工作呢。

    或者,链接的相对路径是否比绝对路径更实用?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Craig    6 年前

    试试这个:

    cd ~
    mkdir bin
    export PATH=$PATH:~/bin                 # Need absolute path to bin
    mkdir -p projects
    cd projects
    echo 'echo "hello world"' > hello.sh    # If the script is just hello world
                                            # this will become an infinite loop
    chmod +x hello.sh
    ln -s "$PWD/hello.sh" ~/bin/hello       # the symbolic link in this case
                                            # needs to be a absolute path
    hello