通过命令行找出符号链接目标

Jar*_*red 143 linux command-line-interface symbolic-link

假设我设置了一个符号链接:

ln -s /root/Public/mytextfile.txt /root/Public/myothertextfile.txt
Run Code Online (Sandbox Code Playgroud)

有没有办法myothertextfile.txt使用命令行查看目标是什么?

bri*_*zil 187

使用该-f标志打印规范化版本。例如:

readlink -f /root/Public/myothertextfile.txt
Run Code Online (Sandbox Code Playgroud)

来自man readlink

-f, --canonicalize
      canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist
Run Code Online (Sandbox Code Playgroud)

  • 正如 [another question](https://superuser.com/questions/330199/how-get-full-path-to-target-of-link) 中所建议的,您可能需要使用 `readlink -f`。 (8认同)
  • 要使其在 Mac OS X 上运行,请使用 `brew install coreutils`。这将安装以字母 `g` 为前缀的基本 gnu 版本的命令,即 `greadlink -f somefile` (5认同)

Kyl*_*ndt 24

readlink 是您想要的命令。您应该查看该命令的手册页。因为如果你想跟随一系列符号链接到实际文件,那么你需要 -e 或 -f 开关:

$ ln -s foooooo zipzip   # fooooo doesn't actually exist
$ ln -s zipzip zapzap

$ # Follows it, but doesn't let you know the file doesn't actually exist
$ readlink -f zapzap
/home/kbrandt/scrap/foooooo

$ # Follows it, but file not there
$ readlink -e zapzap

$ # Follows it, but just to the next symlink
$ readlink zapzap
zipzip
Run Code Online (Sandbox Code Playgroud)


Den*_*son 6

这也将起作用:

ls -l /root/Public/myothertextfile.txt
Run Code Online (Sandbox Code Playgroud)

readlink更适合在脚本中使用而不是解析ls.


Rob*_*mer 5

如果要显示链接的来源和目的地,请尝试stat -c%N files*。例如

$ stat -c%N /dev/fd/*
‘/dev/fd/0’ -> ‘/dev/pts/4’
‘/dev/fd/1’ -> ‘/dev/pts/4’
Run Code Online (Sandbox Code Playgroud)

它不利于解析(用于解析readlink),但它显示链接名称和目的地,没有混乱ls -l

-c可以写成--format%N意思是“引用的文件名,如果是符号链接,则取消引用”。