Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Converting relative path to absolute path in Bash
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
psy_ill
Tux's lil' helper
Tux's lil' helper


Joined: 06 Jul 2003
Posts: 106

PostPosted: Fri May 20, 2005 8:41 am    Post subject: Converting relative path to absolute path in Bash Reply with quote

This is a neat trick which has its uses if you are dealing with softlinks. I needed it just the other day, and it took me a while to figure out how to achieve it. If someone else has a better way to do this, I would be glad to see it, as I find my own solution a bit clumsy.
Code:

# Convert relative paths to absolute
ABSOLUTEPATH=$(cd `dirname "$RELATIVEPATH"` && pwd)"/"`basename "$RELATIVEPATH"`

The script above has a drawback though; the trailing slash if you try to convert a path to a directory. Still, it will function.
Back to top
View user's profile Send private message
zmedico
Developer
Developer


Joined: 02 Jan 2004
Posts: 338
Location: California USA

PostPosted: Fri May 20, 2005 9:38 am    Post subject: Reply with quote

readlink -f has a similar function. The canonical path is often as good as any absolute path. The bash builtin "cd" has a -P parameter that would make your code behave like readlink -f.
_________________
Zac
Back to top
View user's profile Send private message
psy_ill
Tux's lil' helper
Tux's lil' helper


Joined: 06 Jul 2003
Posts: 106

PostPosted: Mon May 23, 2005 10:27 am    Post subject: Reply with quote

Ah, nice. I thought readlink only operated on softlinks.
Still, the trick is useful when you haven't got readlink, for example at the sun systems at my university.
Back to top
View user's profile Send private message
slycordinator
Advocate
Advocate


Joined: 31 Jan 2004
Posts: 3065
Location: Korea

PostPosted: Mon May 23, 2005 4:04 pm    Post subject: Reply with quote

An easier way is to just do:
Code:
`which pwd`

note that those are back-quotes/back-ticks. It's found on the key to the left of the number 1.

This tells it to use the system command pwd rather than the built-in bash command.
Back to top
View user's profile Send private message
frenkel
Veteran
Veteran


Joined: 13 May 2003
Posts: 1034
Location: .nl

PostPosted: Mon May 23, 2005 4:09 pm    Post subject: Reply with quote

slycordinator wrote:
An easier way is to just do:
Code:
`which pwd`

note that those are back-quotes/back-ticks. It's found on the key to the left of the number 1.

This tells it to use the system command pwd rather than the built-in bash command.


Thanks! Great tip.
Back to top
View user's profile Send private message
Gherald
Veteran
Veteran


Joined: 23 Aug 2004
Posts: 1399
Location: CLUAConsole

PostPosted: Mon May 23, 2005 4:11 pm    Post subject: Reply with quote

Code:
emerge realpath
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum