Bash:如何获得符号链接的真实路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29789204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: how to get real path of a symlink?
提问by sensorario
Is it possible, executing a file symlinked in /usr/local/bin folder, to get the absolute path of original script? Well, .. I know where original file is, and I know it because I am linkging it. But, ... I want this script working, even if I move original source code (and symlink).
是否有可能在 /usr/local/bin 文件夹中执行符号链接的文件来获取原始脚本的绝对路径?嗯,.. 我知道原始文件在哪里,我知道它是因为我正在链接它。但是,...我希望这个脚本可以工作,即使我移动了原始源代码(和符号链接)。
#!/bin/bash
echo "my path is ..."
回答by kojiro
readlink
is not a standard command, but it's common on Linux and BSD, including OS X, and it's the most straightforward answer to your question. BSD and GNU readlink implementations are different, so read the documentation for the one you have.
readlink
不是标准命令,但它在 Linux 和 BSD(包括 OS X)上很常见,而且它是对您的问题最直接的回答。BSD 和 GNU readlink 实现是不同的,因此请阅读您所拥有的文档。
If readlink
is not available, or you need to write a cross-platform script that isn't bound to a specific implementation:
如果readlink
不可用,或者您需要编写未绑定到特定实现的跨平台脚本:
If the symlink is also a directory, then
如果符号链接也是目录,则
cd -P "$symlinkdir"
will get you into the dereferenced directory, so
会让你进入取消引用的目录,所以
echo "I am in $(cd -P "$symlinkdir" && pwd)"
will echo the fully dereferenced directory. That said, cd -P
dereferences the entire path, so if you have more than one symlink in the same path you can have unexpected results.
将回显完全取消引用的目录。也就是说,cd -P
取消引用整个路径,因此如果在同一路径中有多个符号链接,则可能会出现意外结果。
If the symlink is to a file, not a directory, you may not need to dereference the link. Most commands follow symlinks harmlessly. If you simply want to check if a file is a link, use test -L
.
如果符号链接是指向文件而不是目录,则您可能不需要取消引用该链接。大多数命令都无害地遵循符号链接。如果您只想检查文件是否为链接,请使用test -L
.
回答by Arunas Bartisius
lets assume we have real script or file and symbolic link to it:
让我们假设我们有真正的脚本或文件以及指向它的符号链接:
$ ls -la
-rwxr-xr-x 1 root root 0 Mar 20 07:05 realscript.sh
lrwxrwxrwx 1 root root 10 Mar 20 07:05 symlink -> realscript.sh
And the part of GNU coreutils are few very usefull commands:
GNU coreutils 的部分是一些非常有用的命令:
$ realpath symlink
/home/test/realscript.sh
see also on original:
另见原文:
realpath realscript.sh
/home/test/realscript.sh
Also very good combination in scripting is to use dirname
on saved
脚本中的另一个很好的组合是用于dirname
保存
$ dirname /home/test/realscript.sh
/home/test
so to wrap it up, you can use in script
所以总结一下,你可以在脚本中使用
echo $( dirname $(realpath "symlink") )
or to get and store in variable real script home dir:
或者获取并存储在可变的真实脚本主目录中:
script_home=$( dirname $(realpath "/$ /home/test2/symlink
/home/test
Original script home: /home/test
Original script is: /home/test/realscript.sh
Called script is: /home/test2/symlink
") )
echo Original script home: $script_home
To test everything, we put symlink into /home/test2/, amend some additional things and run/call it from root directory:
为了测试所有内容,我们将符号链接放入 /home/test2/,修改一些其他内容并从根目录运行/调用它:
$ find filename -printf %l
Please try to write your self the amended outputs :)
请尝试将修改后的输出写给自己:)
回答by Manuel Sánchez Mendoza
if you are working on Linux readlink -f $LINK
and on mac you can also use that greadlink -f $LINK
如果你在 Linuxreadlink -f $LINK
和 mac上工作,你也可以使用它greadlink -f $LINK
回答by David
To get the symlink path even if it is broken:
即使它已损坏,也要获取符号链接路径:
##代码##