LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   relative to absolute path (https://www.linuxquestions.org/questions/programming-9/relative-to-absolute-path-403194/)

vishalbutte 01-14-2006 12:48 AM

relative to absolute path
 
how to get the absolute path from relative path in shell script? is there any other command and function than pwd.?


thanks in advance

paragn 01-14-2006 01:20 AM

hi,
I dont think its possible to get absolute path from relative path as the same name of file may exist to any other location so you will get all those entries of given relative path of filename.

Hko 01-14-2006 04:39 AM

Quote:

Originally Posted by paragn
I dont think its possible to get absolute path from relative path as the same name of file may exist to any other location so you will get all those entries of given relative path of filename.

No. It is possible. A relative path is relative to the current working directory. So there is only one full path for a relative path.

Debian comes with a shell utility called "realpath", but it's not installed by default, and not all distributions provide it. The program "realpath" also resolves "..", "." and symbolic links to get the one-and-only full path.

If you really need to, such program is quite easy to make, because there is a "realpath()" C-function in the standard C-library. See "man 3 realpath".

paragn 01-14-2006 05:28 AM

hi Hko,
oh i was not knowing this thing. Thanks for the info

randyding 01-14-2006 03:17 PM

try this,
Code:

p="../../dirname/filename.bin"
wdsave="`pwd`"
cd "`dirname "$p"`"
absolutepath="`pwd`/`basename $p`"
cd "$wdsave"



All times are GMT -5. The time now is 10:57 AM.