konyak
konyak

Reputation: 11716

Change directory to location of executable

How do I change directory to location of executable from a Linux terminal?

I tried, e.g., cd $(which ruby)/.. but that doesn't work.

Upvotes: 1

Views: 76

Answers (2)

Zereges
Zereges

Reputation: 5209

which command tells you not only directory name but also filename, so you have to get rid of the filename using some utility. I used simple sed for this to remove everything after last slash including.

This works in bash:

cd `which ls | sed -r 's#/[^/]+$##'`

Upvotes: 0

David Hoelzer
David Hoelzer

Reputation: 16381

This should work for you:

cd $(dirname `which ruby`)

Upvotes: 1

Related Questions