13 March 2008

get absolute path from relative path

I think this is a quite common issue, but googling for it doesn't (easily) get the very useful solution I finally found. So I put it here.
The solution is really easy and it's called readlink.
Given a relative path like../symLink/myDir, simply type
readlink -f ../symLink/myDir
and you will get something similar to
/root/path/to/symLinkTarget/myDir
Of course you can also get the full path for a file instead of a directory (myDir → myFile.ext still works). The only point, as it is understood in my example, is that symbolic links are followed, so you cannot get the "symbolic" path to your dir/file. But this could be view as a feature. Suppose you want to know the full real path of your sym-link-reached working directory, you can define a very simple modified pwd command alias like this
alias Pwd='readlink -f .'
and compare its output with the usual pwd output.
Enjoy!

0 comments: