29

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?

JFMR
  • 23,265
  • 4
  • 52
  • 76
Jay
  • 24,173
  • 25
  • 93
  • 141
  • Since this was tagged with `c`, this is probably a duplicate of http://stackoverflow.com/questions/229012/getting-absolute-path-of-a-file – bfontaine Oct 01 '13 at 16:13

7 Answers7

24

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.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
unwind
  • 391,730
  • 64
  • 469
  • 606
  • Any given file can, for sure. Hard-links to directories isn't necessarily supported. Symlinks also cause some confusion when it come sto determining the "real" path. – Vatine Feb 26 '10 at 13:28
  • @unwind, Thanks for the Info. If due to hard links, if a given directory resolves to multiple different absolute paths, what will be the behaviour of realpath API? – Jay Feb 26 '10 at 13:35
  • 4
    Hardlinks to directories are considered evil and forbidden by most filesystems. – edgar.holleis Feb 26 '10 at 13:36
  • I am a little confused. Is it possible that a given directory resolves to many different absolute paths? If yes, then what will be the behaviour realpath? – Jay Feb 26 '10 at 13:55
  • @Jay it doesn't affect resolving a relative path to an absolute path. – Otto Allmendinger Feb 26 '10 at 19:56
  • `realpath` resolves the name in the way you would expect - by (recursively) flattening out `/../` and `/./` components and resolving symlinks. The "multiple paths" issue just means that the name you get out at the end isn't necessarily unique for a given file (since a file does not necessarily have a single "true name"). – caf Feb 27 '10 at 07:00
  • Of course NTFS (i.e. Windows) also has hard links with all the same complexities: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365006(v=vs.85).aspx. – bart Jan 04 '12 at 09:39
  • There seems to be no way to to resolve a path to a given absolute base path other then the current working directory. Oh men, UNIX stinks sometimes, so i have to roll out my own function. – Lothar Jul 18 '12 at 00:01
19

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; 
} 
Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
  • 1
    Please use PATH_MAX instead of 100 – Speed8ump Feb 23 '15 at 16:09
  • 7
    This code is insecure and buggy, as pointed out above. Do **not** give such a small buffer to `realpath` which will most likely write beyond the buffer size (since it *requires* a length of PATH_MAX). Even if the program doesn't crash, this code could lead to security vulnerabilities depending on the variable layout, if an adversary can control the original path to be resolved. The manual recommends passing NULL as a second parameter and letting `realpath` allocate memory to ensure no issues with PATH_MAX definitions, starting with POSIX 2008. – Steve Dodier-Lazaro Aug 09 '15 at 14:30
  • 2
    I think it is a good think to always remember that PATH_MAX simply isn't: http://insanecoding.blogspot.com.br/2007/11/pathmax-simply-isnt.html – Ciro Costa Aug 13 '15 at 17:45
6

Try realpath:

$ man realpath

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

Paul R
  • 208,748
  • 37
  • 389
  • 560
3

There is the realpath from stdlib.h

Otto Allmendinger
  • 27,448
  • 7
  • 68
  • 79
  • 3
    I immediately thought of realpath, too, but I was *stunned* -- *stunned* I say -- when I saw your answer showing that `realpath` is in `stdlib.h`. Surely that *can't* be true, considering that `realpath` is not part of the C library. Lo and behold, it's true. I'm dumbfounded. What's a well-formed program that defines its own function named `realpath` to do? Those POSIX guys have run amok! *Amok* I say! – Dan Moulding Feb 26 '10 at 22:25
  • Dan: So long as they invoke their compiler in "strictly conforming" mode and don't define any macros that invoke undefined behaviour (like `_XOPEN_SOURCE`), they should be OK. – caf Feb 27 '10 at 07:02
2

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 .
Lionel
  • 33,274
  • 1
  • 16
  • 5
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.

Zebooka
  • 380
  • 2
  • 10
-3
// For C++ with Gnome Gtkmm3 libraries
#include <glibmm.h>
#include <giomm.h>

  string PathRel2Abs(string relpath) {
  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(relpath);
  return file->get_path();
}
Bimo
  • 5,987
  • 2
  • 39
  • 61