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?
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?
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.
Check out the realpath function.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char resolved_path[100];
realpath("../../", resolved_path);
printf("\n%s\n",resolved_path);
return 0;
}
The is also another useful way, like "readlink -m $filename"
First of all, it works without requirement for target file to exist. Secondly, it will handle symlinks and get really real path.