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)


1 Answer 1


you can use dirname to strip path

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

using xargs

cd "$(which chromedriver | xargs dirname)"
  • 1
    Or cd $(dirname $(which chromedriver)) Commented Aug 5, 2020 at 4:32
  • 1
    Quote. cd "$(dirname "$FILEPATH")" Commented Aug 5, 2020 at 4:33

Not the answer you're looking for? Browse other questions tagged .