Alex Rivera | Logout

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

Asked 2010-02-26T13:17:15.767
27

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?

Edit
Report

2 Answers

25

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.

answered 2010-02-26T13:22:55.207
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 .
answered 2012-10-10T19:07:02.207

Your Answer