How to get the absolute path for a given relative path programmatically in Linux? How to get the absolute path for a given relative path programmatically in Linux? linux linux

How to get the absolute path for a given relative path programmatically 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> #include <linux/limits.h>int main() {         char resolved_path[PATH_MAX];         realpath("../../", resolved_path);         printf("\n%s\n",resolved_path);         return 0; } 


Try realpath:

$ man realpath

This is also available in BSD, OS X, et al.