如何获得符号 link 的名称?

How can I get the name of a symbolic link?

命令的输出看起来像这样

# ls -l abc.zip
lrwxrwxrwx 1 sri dba 122 Mar 27 23:37 /a/b/c/abc.zip -> /x/y/z/abc.zip

我需要 extract/cut -> 之后的整个路径。我用过 cut -f -d 但它有时不起作用,我猜列号正在改变。所以我需要 sed 等价物。

这可能适合您 (GNU sed):

sed -n 's/^l.*->\s*//p' file

使用 'cut' 您只能指定单个字符分隔符。

对于字符串定界符,一个简单明了的选项是 awk。在你的情况下尝试:

ls -ls abc.zip | awk -F '->' '{print }'

您正在阅读的是关于符号link的信息。 Parsing ls is definitely a bad idea.

使用 readlink 怎么样?它 读取符号的值 link:

readlink abc.zip

或使用 -f 作为完整路径:

readlink -f abc.zip

看例子:

$ touch a         
$ ln -s a b          # we create a symbolic link "b" to the file "a"
$ ls -l b
lrwxrwxrwx 1 me me 1 Apr  6 09:54 b -> a
$ readlink -f "b"    # we check the full "destination" of the symlink "b"
/home/me/a
$ readlink "b"       # we check the "destination" of the symlink "b"
a