获取它指向的链接路径?

时间:2023-01-23 13:07:35

Is it possible to get the abolute path of the link that it is pointing to? Is there any simple system command?

是否有可能获得它所指向的链接的绝对路径?有没有简单的系统命令?

I need for all of the following OS HP-UX 11i, 1123u, 1123i AIX 5.2 and 5.3 Suse Linux 10 Solaris 10

我需要以下所有操作系统HP-UX 11i,1123u,1123i AIX 5.2和5.3 Suse Linux 10 Solaris 10

2 个解决方案

#1


You didn't specify a language, so I assume you want a command that can be run in whatever shell you are using. The ls command has the -l (that is an ell) option which prints out a lot of information about the file. The last bit of information is the full path, so you should be able to say

您没有指定语言,因此我假设您需要一个可以在您正在使用的shell中运行的命令。 ls命令具有-l(即一个ell)选项,可以打印出有关该文件的大量信息。最后一点信息是完整路径,所以你应该可以说

ls -l file | awk '{print $NF}'

on any SUS2 compliant machine (which should be all of the commercial UNIXes). This will have a problem if the file or the any of the directories leading up to the file have spaces though.

在任何符合SUS2的机器上(应该是所有商业UNIX)。如果文件或导致该文件的任何目录都有空格,则会出现问题。

#2


If you are looking for a system call, you want readlink(2). This is standardized, and so should be available on all POSIX compliant systems.

如果您正在寻找系统调用,则需要readlink(2)。这是标准化的,因此应该适用于所有符合POSIX标准的系统。

Here's an example of its usage, taken from the link given earlier:

以下是其使用示例,取自前面给出的链接:

#include <unistd.h>

char buf[1024];
ssizet_t len;

if ((len = readlink("/modules/pass1", buf, sizeof(buf)-1)) != -1)
    buf[len] = '\0';

If you're looking for a command line utility, it doesn't look like there is one standardized, but GNU (Linux) and BSD both have readlink(1).

如果您正在寻找一个命令行实用程序,它看起来不像有一个标准化,但GNU(Linux)和BSD都有readlink(1)。

#1


You didn't specify a language, so I assume you want a command that can be run in whatever shell you are using. The ls command has the -l (that is an ell) option which prints out a lot of information about the file. The last bit of information is the full path, so you should be able to say

您没有指定语言,因此我假设您需要一个可以在您正在使用的shell中运行的命令。 ls命令具有-l(即一个ell)选项,可以打印出有关该文件的大量信息。最后一点信息是完整路径,所以你应该可以说

ls -l file | awk '{print $NF}'

on any SUS2 compliant machine (which should be all of the commercial UNIXes). This will have a problem if the file or the any of the directories leading up to the file have spaces though.

在任何符合SUS2的机器上(应该是所有商业UNIX)。如果文件或导致该文件的任何目录都有空格,则会出现问题。

#2


If you are looking for a system call, you want readlink(2). This is standardized, and so should be available on all POSIX compliant systems.

如果您正在寻找系统调用,则需要readlink(2)。这是标准化的,因此应该适用于所有符合POSIX标准的系统。

Here's an example of its usage, taken from the link given earlier:

以下是其使用示例,取自前面给出的链接:

#include <unistd.h>

char buf[1024];
ssizet_t len;

if ((len = readlink("/modules/pass1", buf, sizeof(buf)-1)) != -1)
    buf[len] = '\0';

If you're looking for a command line utility, it doesn't look like there is one standardized, but GNU (Linux) and BSD both have readlink(1).

如果您正在寻找一个命令行实用程序,它看起来不像有一个标准化,但GNU(Linux)和BSD都有readlink(1)。