How do you determine the path of the file that a symlink points to?

孤人 提交于 2019-12-05 09:58:11

The readlink function. Do a man 2 readlink. This function is part of the Posix API, so it should work on almost any Unix.

If the path starts with a '/', then its an absolute symlink and you have the full absolute path of the file to which it refers (which may be another symbolic link, and you'll have to repeat the process again).

If the path does not start with '/', then it's a relative link and is interpreted relative to the directory the symlink is in. It's up to you to combine the path appropriately to figure out the real filename.

Of course, sometimes symlinks are used to store information that's not actually a path. Sometimes programs store a PID or other such information in them for locking purposes. The calls to create a symlink are one of the more reliably atomic operations that can be performed on an NFS mounted filesystem.

Also, on my Fedora system, if you do man 7 symlink you get a long discussion of symlinks, their behavior in various circumstances and a rundown of the functions that manipulate them or are affected by them.

On POSIX or BSD system use readlink()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!