How to get the absolute path for a given relative path programmatically in Linux?
63,852
Solution 1
As Paul mentioned, use realpath()
. Please note though, that since many file systems in Linux support hard links, any given directory can have a number of different absolute paths.
Solution 2
Check out the realpath function.
#include <stdlib.h>
#include <stdio.h>
#include <linux/limits.h>
int main()
{
char resolved_path[PATH_MAX];
realpath("../../", resolved_path);
printf("\n%s\n",resolved_path);
return 0;
}
Solution 3
Try realpath:
$ man realpath
This is also available in BSD, OS X, et al.
Solution 4
There is the realpath
from stdlib.h
Solution 5
Running on RedHat 5.3, realpath doesn't exist but readlink is installed. You can use it on relative paths and symlinks, plus it will resolve symlinks recursively for you. It's thus a better option that realpath in my opinion
readlink -f .
Comments
-
Jay almost 2 years
How to get the absolute path for a given relative path programmatically in Linux?
Incase of Windows we have the
_fullpath()
API. In other words, I mean what is analogous API to_fullpath
of Windows in Linux?