1

I'm trying to navigate to the directory in which a file resides. i.e. I'm trying to

cd $(which chromedriver)

That errors for obvious reasons cd: not a directory: /usr/local/bin/chromedriver

Is there either

  1. An alternative to cd that navigates to the directory of a file, or

  2. A way to handle or parse the output of which so that it's generalisable (i.e. not just removing the last 10 characters in the case of chromedriver, but which works for all applications)

stevec
  • 181

1 Answers1

3

you can use dirname to strip path

cd "$(dirname "$(which chromedriver)")"

using xargs

cd "$(which chromedriver | xargs dirname)"
terdon
  • 242,166
Akhil
  • 1,290