Search code examples
cls

How to get the path of the final final point by a symbolic link using `stat`


I try to get the name of the folder / file pointed by the symbolic link.

For example, when you do ls -l You can get this result

-rw-r--r--  1 macos  staff  0 Feb 22 12:05 test
lrwxr-xr-x  1 macos  staff  7 Feb 19 11:05 sl -> test

How could I do, if I know the path of sl to get the file/folder here it pointed to ?

I want to do it using stat if possible.

Any suggestion ?


Solution

  • So to summarize :

    It's impossible to get the name of the file/folder pointed by a symbolic link, using lstat or stat.

    The function readlink can do the job => Man : http://man7.org/linux/man-pages/man2/readlink.2.html. You will get example in the man

    Thank you for helping me solve my problem