Linux 有没有办法查看符号链接的实际内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4572805/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:20:29  来源:igfitidea点击:

is there a way to see the actual contents of a symlink?

linuxshellfilesystemssymlinkcat

提问by fabio

When you do

当你做

cat some-symlink-to-some-real-file

it shows the contents of the real file, not what is within the symlink itself. Is there a way to see what's actually in it?

它显示真实文件的内容,而不是符号链接本身的内容。有没有办法查看其中的实际内容?

采纳答案by PleaseStand

The ls -lcommand will show you that:

ls -l命令将显示:

$ ls -l foo
lrwxrwxrwx 1 user group 11 2010-12-31 19:49 foo -> /etc/passwd

Or the readlinkcommand:

readlink命令

$ readlink foo
/etc/passwd

So, the symbolic link foopoints to the path /etc/passwd.

因此,符号链接foo指向 path /etc/passwd

回答by Eric Fortis

Try

尝试

find . -type l -exec ls -la {} \;

回答by Ben Voigt

You can call the readlink(2)function, which will place the linked-to name into a buffer.

您可以调用该readlink(2)函数,该函数会将链接到的名称放入缓冲区。

Note that the result has a length (stored in the return value) rather than being NUL-terminated. So if you want to use it as a string, append a NUL yourself.

请注意,结果具有长度(存储在返回值中)而不是 NUL 终止。因此,如果您想将其用作字符串,请自己附加一个 NUL。

Most higher-level/scripting languages, such as perl or python, will provide a readlink wrapper that converts to the usual language-appropriate string type, so you won't be bothered by details such as NUL-termination.

大多数高级/脚本语言,例如 perl 或 python,将提供一个 readlink 包装器,可以转换为通常的语言合适的字符串类型,因此您不会被诸如 NUL 终止之类的细节所困扰。

回答by Pavel Patrin

Regarding to man page http://man7.org/linux/man-pages/man7/symlink.7.htmlsymlink is regular file (with special flag) with path to target in its content. So you could copy symlink to FAT partition and read it content there.

关于手册页http://man7.org/linux/man-pages/man7/symlink.7.html符号链接是常规文件(带有特殊标志),其内容中带有目标路径。所以你可以将符号链接复制到 FAT 分区并在那里读取它的内容。